aoqi@0: /* aoqi@0: * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psAdaptiveSizePolicy.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psGCAdaptivePolicyCounters.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psScavenge.hpp" aoqi@0: #include "gc_implementation/shared/gcPolicyCounters.hpp" aoqi@0: #include "gc_interface/gcCause.hpp" aoqi@0: #include "memory/collectorPolicy.hpp" aoqi@0: #include "runtime/timer.hpp" aoqi@0: #include "utilities/top.hpp" aoqi@0: aoqi@0: #include aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: PSAdaptiveSizePolicy::PSAdaptiveSizePolicy(size_t init_eden_size, aoqi@0: size_t init_promo_size, aoqi@0: size_t init_survivor_size, aoqi@0: size_t space_alignment, aoqi@0: double gc_pause_goal_sec, aoqi@0: double gc_minor_pause_goal_sec, aoqi@0: uint gc_cost_ratio) : aoqi@0: AdaptiveSizePolicy(init_eden_size, aoqi@0: init_promo_size, aoqi@0: init_survivor_size, aoqi@0: gc_pause_goal_sec, aoqi@0: gc_cost_ratio), aoqi@0: _collection_cost_margin_fraction(AdaptiveSizePolicyCollectionCostMargin / 100.0), aoqi@0: _space_alignment(space_alignment), aoqi@0: _live_at_last_full_gc(init_promo_size), aoqi@0: _gc_minor_pause_goal_sec(gc_minor_pause_goal_sec), aoqi@0: _latest_major_mutator_interval_seconds(0), aoqi@0: _young_gen_change_for_major_pause_count(0) aoqi@0: { aoqi@0: // Sizing policy statistics aoqi@0: _avg_major_pause = aoqi@0: new AdaptivePaddedAverage(AdaptiveTimeWeight, PausePadding); aoqi@0: _avg_minor_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight); aoqi@0: _avg_major_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight); aoqi@0: aoqi@0: _avg_base_footprint = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight); aoqi@0: _major_pause_old_estimator = aoqi@0: new LinearLeastSquareFit(AdaptiveSizePolicyWeight); aoqi@0: _major_pause_young_estimator = aoqi@0: new LinearLeastSquareFit(AdaptiveSizePolicyWeight); aoqi@0: _major_collection_estimator = aoqi@0: new LinearLeastSquareFit(AdaptiveSizePolicyWeight); aoqi@0: aoqi@0: _young_gen_size_increment_supplement = YoungGenerationSizeSupplement; aoqi@0: _old_gen_size_increment_supplement = TenuredGenerationSizeSupplement; aoqi@0: aoqi@0: // Start the timers aoqi@0: _major_timer.start(); aoqi@0: aoqi@0: _old_gen_policy_is_ready = false; aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::calculate_free_based_on_live(size_t live, uintx ratio_as_percentage) { aoqi@0: // We want to calculate how much free memory there can be based on the aoqi@0: // amount of live data currently in the old gen. Using the formula: aoqi@0: // ratio * (free + live) = free aoqi@0: // Some equation solving later we get: aoqi@0: // free = (live * ratio) / (1 - ratio) aoqi@0: aoqi@0: const double ratio = ratio_as_percentage / 100.0; aoqi@0: const double ratio_inverse = 1.0 - ratio; aoqi@0: const double tmp = live * ratio; aoqi@0: size_t free = (size_t)(tmp / ratio_inverse); aoqi@0: aoqi@0: return free; aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::calculated_old_free_size_in_bytes() const { aoqi@0: size_t free_size = (size_t)(_promo_size + avg_promoted()->padded_average()); aoqi@0: size_t live = ParallelScavengeHeap::heap()->old_gen()->used_in_bytes(); aoqi@0: aoqi@0: if (MinHeapFreeRatio != 0) { aoqi@0: size_t min_free = calculate_free_based_on_live(live, MinHeapFreeRatio); aoqi@0: free_size = MAX2(free_size, min_free); aoqi@0: } aoqi@0: aoqi@0: if (MaxHeapFreeRatio != 100) { aoqi@0: size_t max_free = calculate_free_based_on_live(live, MaxHeapFreeRatio); aoqi@0: free_size = MIN2(max_free, free_size); aoqi@0: } aoqi@0: aoqi@0: return free_size; aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::major_collection_begin() { aoqi@0: // Update the interval time aoqi@0: _major_timer.stop(); aoqi@0: // Save most recent collection time aoqi@0: _latest_major_mutator_interval_seconds = _major_timer.seconds(); aoqi@0: _major_timer.reset(); aoqi@0: _major_timer.start(); aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::update_minor_pause_old_estimator( aoqi@0: double minor_pause_in_ms) { aoqi@0: double promo_size_in_mbytes = ((double)_promo_size)/((double)M); aoqi@0: _minor_pause_old_estimator->update(promo_size_in_mbytes, aoqi@0: minor_pause_in_ms); aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::major_collection_end(size_t amount_live, aoqi@0: GCCause::Cause gc_cause) { aoqi@0: // Update the pause time. aoqi@0: _major_timer.stop(); aoqi@0: aoqi@0: if (gc_cause != GCCause::_java_lang_system_gc || aoqi@0: UseAdaptiveSizePolicyWithSystemGC) { aoqi@0: double major_pause_in_seconds = _major_timer.seconds(); aoqi@0: double major_pause_in_ms = major_pause_in_seconds * MILLIUNITS; aoqi@0: aoqi@0: // Sample for performance counter aoqi@0: _avg_major_pause->sample(major_pause_in_seconds); aoqi@0: aoqi@0: // Cost of collection (unit-less) aoqi@0: double collection_cost = 0.0; aoqi@0: if ((_latest_major_mutator_interval_seconds > 0.0) && aoqi@0: (major_pause_in_seconds > 0.0)) { aoqi@0: double interval_in_seconds = aoqi@0: _latest_major_mutator_interval_seconds + major_pause_in_seconds; aoqi@0: collection_cost = aoqi@0: major_pause_in_seconds / interval_in_seconds; aoqi@0: avg_major_gc_cost()->sample(collection_cost); aoqi@0: aoqi@0: // Sample for performance counter aoqi@0: _avg_major_interval->sample(interval_in_seconds); aoqi@0: } aoqi@0: aoqi@0: // Calculate variables used to estimate pause time vs. gen sizes aoqi@0: double eden_size_in_mbytes = ((double)_eden_size)/((double)M); aoqi@0: double promo_size_in_mbytes = ((double)_promo_size)/((double)M); aoqi@0: _major_pause_old_estimator->update(promo_size_in_mbytes, aoqi@0: major_pause_in_ms); aoqi@0: _major_pause_young_estimator->update(eden_size_in_mbytes, aoqi@0: major_pause_in_ms); aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print("psAdaptiveSizePolicy::major_collection_end: " aoqi@0: "major gc cost: %f average: %f", collection_cost, aoqi@0: avg_major_gc_cost()->average()); aoqi@0: gclog_or_tty->print_cr(" major pause: %f major period %f", aoqi@0: major_pause_in_ms, aoqi@0: _latest_major_mutator_interval_seconds * MILLIUNITS); aoqi@0: } aoqi@0: aoqi@0: // Calculate variable used to estimate collection cost vs. gen sizes aoqi@0: assert(collection_cost >= 0.0, "Expected to be non-negative"); aoqi@0: _major_collection_estimator->update(promo_size_in_mbytes, aoqi@0: collection_cost); aoqi@0: } aoqi@0: aoqi@0: // Update the amount live at the end of a full GC aoqi@0: _live_at_last_full_gc = amount_live; aoqi@0: aoqi@0: // The policy does not have enough data until at least some major collections aoqi@0: // have been done. aoqi@0: if (_avg_major_pause->count() >= AdaptiveSizePolicyReadyThreshold) { aoqi@0: _old_gen_policy_is_ready = true; aoqi@0: } aoqi@0: aoqi@0: // Interval times use this timer to measure the interval that aoqi@0: // the mutator runs. Reset after the GC pause has been measured. aoqi@0: _major_timer.reset(); aoqi@0: _major_timer.start(); aoqi@0: } aoqi@0: aoqi@0: // If the remaining free space in the old generation is less that aoqi@0: // that expected to be needed by the next collection, do a full aoqi@0: // collection now. aoqi@0: bool PSAdaptiveSizePolicy::should_full_GC(size_t old_free_in_bytes) { aoqi@0: aoqi@0: // A similar test is done in the scavenge's should_attempt_scavenge(). If aoqi@0: // this is changed, decide if that test should also be changed. aoqi@0: bool result = padded_average_promoted_in_bytes() > (float) old_free_in_bytes; aoqi@0: if (PrintGCDetails && Verbose) { aoqi@0: if (result) { aoqi@0: gclog_or_tty->print(" full after scavenge: "); aoqi@0: } else { aoqi@0: gclog_or_tty->print(" no full after scavenge: "); aoqi@0: } aoqi@0: gclog_or_tty->print_cr(" average_promoted " SIZE_FORMAT aoqi@0: " padded_average_promoted " SIZE_FORMAT aoqi@0: " free in old gen " SIZE_FORMAT, aoqi@0: (size_t) average_promoted_in_bytes(), aoqi@0: (size_t) padded_average_promoted_in_bytes(), aoqi@0: old_free_in_bytes); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::clear_generation_free_space_flags() { aoqi@0: aoqi@0: AdaptiveSizePolicy::clear_generation_free_space_flags(); aoqi@0: aoqi@0: set_change_old_gen_for_min_pauses(0); aoqi@0: aoqi@0: set_change_young_gen_for_maj_pauses(0); aoqi@0: } aoqi@0: aoqi@0: // If this is not a full GC, only test and modify the young generation. aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::compute_generations_free_space( aoqi@0: size_t young_live, aoqi@0: size_t eden_live, aoqi@0: size_t old_live, aoqi@0: size_t cur_eden, aoqi@0: size_t max_old_gen_size, aoqi@0: size_t max_eden_size, aoqi@0: bool is_full_gc) { aoqi@0: compute_eden_space_size(young_live, aoqi@0: eden_live, aoqi@0: cur_eden, aoqi@0: max_eden_size, aoqi@0: is_full_gc); aoqi@0: aoqi@0: compute_old_gen_free_space(old_live, aoqi@0: cur_eden, aoqi@0: max_old_gen_size, aoqi@0: is_full_gc); aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::compute_eden_space_size( aoqi@0: size_t young_live, aoqi@0: size_t eden_live, aoqi@0: size_t cur_eden, aoqi@0: size_t max_eden_size, aoqi@0: bool is_full_gc) { aoqi@0: aoqi@0: // Update statistics aoqi@0: // Time statistics are updated as we go, update footprint stats here aoqi@0: _avg_base_footprint->sample(BaseFootPrintEstimate); aoqi@0: avg_young_live()->sample(young_live); aoqi@0: avg_eden_live()->sample(eden_live); aoqi@0: aoqi@0: // This code used to return if the policy was not ready , i.e., aoqi@0: // policy_is_ready() returning false. The intent was that aoqi@0: // decisions below needed major collection times and so could aoqi@0: // not be made before two major collections. A consequence was aoqi@0: // adjustments to the young generation were not done until after aoqi@0: // two major collections even if the minor collections times aoqi@0: // exceeded the requested goals. Now let the young generation aoqi@0: // adjust for the minor collection times. Major collection times aoqi@0: // will be zero for the first collection and will naturally be aoqi@0: // ignored. Tenured generation adjustments are only made at the aoqi@0: // full collections so until the second major collection has aoqi@0: // been reached, no tenured generation adjustments will be made. aoqi@0: aoqi@0: // Until we know better, desired promotion size uses the last calculation aoqi@0: size_t desired_promo_size = _promo_size; aoqi@0: aoqi@0: // Start eden at the current value. The desired value that is stored aoqi@0: // in _eden_size is not bounded by constraints of the heap and can aoqi@0: // run away. aoqi@0: // aoqi@0: // As expected setting desired_eden_size to the current aoqi@0: // value of desired_eden_size as a starting point aoqi@0: // caused desired_eden_size to grow way too large and caused aoqi@0: // an overflow down stream. It may have improved performance in aoqi@0: // some case but is dangerous. aoqi@0: size_t desired_eden_size = cur_eden; aoqi@0: aoqi@0: // Cache some values. There's a bit of work getting these, so aoqi@0: // we might save a little time. aoqi@0: const double major_cost = major_gc_cost(); aoqi@0: const double minor_cost = minor_gc_cost(); aoqi@0: aoqi@0: // This method sets the desired eden size. That plus the aoqi@0: // desired survivor space sizes sets the desired young generation aoqi@0: // size. This methods does not know what the desired survivor aoqi@0: // size is but expects that other policy will attempt to make aoqi@0: // the survivor sizes compatible with the live data in the aoqi@0: // young generation. This limit is an estimate of the space left aoqi@0: // in the young generation after the survivor spaces have been aoqi@0: // subtracted out. aoqi@0: size_t eden_limit = max_eden_size; aoqi@0: aoqi@0: const double gc_cost_limit = GCTimeLimit/100.0; aoqi@0: aoqi@0: // Which way should we go? aoqi@0: // if pause requirement is not met aoqi@0: // adjust size of any generation with average paus exceeding aoqi@0: // the pause limit. Adjust one pause at a time (the larger) aoqi@0: // and only make adjustments for the major pause at full collections. aoqi@0: // else if throughput requirement not met aoqi@0: // adjust the size of the generation with larger gc time. Only aoqi@0: // adjust one generation at a time. aoqi@0: // else aoqi@0: // adjust down the total heap size. Adjust down the larger of the aoqi@0: // generations. aoqi@0: aoqi@0: // Add some checks for a threshold for a change. For example, aoqi@0: // a change less than the necessary alignment is probably not worth aoqi@0: // attempting. aoqi@0: aoqi@0: aoqi@0: if ((_avg_minor_pause->padded_average() > gc_pause_goal_sec()) || aoqi@0: (_avg_major_pause->padded_average() > gc_pause_goal_sec())) { aoqi@0: // aoqi@0: // Check pauses aoqi@0: // aoqi@0: // Make changes only to affect one of the pauses (the larger) aoqi@0: // at a time. aoqi@0: adjust_eden_for_pause_time(is_full_gc, &desired_promo_size, &desired_eden_size); aoqi@0: aoqi@0: } else if (_avg_minor_pause->padded_average() > gc_minor_pause_goal_sec()) { aoqi@0: // Adjust only for the minor pause time goal aoqi@0: adjust_eden_for_minor_pause_time(is_full_gc, &desired_eden_size); aoqi@0: aoqi@0: } else if(adjusted_mutator_cost() < _throughput_goal) { aoqi@0: // This branch used to require that (mutator_cost() > 0.0 in 1.4.2. aoqi@0: // This sometimes resulted in skipping to the minimize footprint aoqi@0: // code. Change this to try and reduce GC time if mutator time is aoqi@0: // negative for whatever reason. Or for future consideration, aoqi@0: // bail out of the code if mutator time is negative. aoqi@0: // aoqi@0: // Throughput aoqi@0: // aoqi@0: assert(major_cost >= 0.0, "major cost is < 0.0"); aoqi@0: assert(minor_cost >= 0.0, "minor cost is < 0.0"); aoqi@0: // Try to reduce the GC times. aoqi@0: adjust_eden_for_throughput(is_full_gc, &desired_eden_size); aoqi@0: aoqi@0: } else { aoqi@0: aoqi@0: // Be conservative about reducing the footprint. aoqi@0: // Do a minimum number of major collections first. aoqi@0: // Have reasonable averages for major and minor collections costs. aoqi@0: if (UseAdaptiveSizePolicyFootprintGoal && aoqi@0: young_gen_policy_is_ready() && aoqi@0: avg_major_gc_cost()->average() >= 0.0 && aoqi@0: avg_minor_gc_cost()->average() >= 0.0) { aoqi@0: size_t desired_sum = desired_eden_size + desired_promo_size; aoqi@0: desired_eden_size = adjust_eden_for_footprint(desired_eden_size, desired_sum); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Note we make the same tests as in the code block below; the code aoqi@0: // seems a little easier to read with the printing in another block. aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: if (desired_eden_size > eden_limit) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "PSAdaptiveSizePolicy::compute_eden_space_size limits:" aoqi@0: " desired_eden_size: " SIZE_FORMAT aoqi@0: " old_eden_size: " SIZE_FORMAT aoqi@0: " eden_limit: " SIZE_FORMAT aoqi@0: " cur_eden: " SIZE_FORMAT aoqi@0: " max_eden_size: " SIZE_FORMAT aoqi@0: " avg_young_live: " SIZE_FORMAT, aoqi@0: desired_eden_size, _eden_size, eden_limit, cur_eden, aoqi@0: max_eden_size, (size_t)avg_young_live()->average()); aoqi@0: } aoqi@0: if (gc_cost() > gc_cost_limit) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "PSAdaptiveSizePolicy::compute_eden_space_size: gc time limit" aoqi@0: " gc_cost: %f " aoqi@0: " GCTimeLimit: %d", aoqi@0: gc_cost(), GCTimeLimit); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Align everything and make a final limit check aoqi@0: desired_eden_size = align_size_up(desired_eden_size, _space_alignment); aoqi@0: desired_eden_size = MAX2(desired_eden_size, _space_alignment); aoqi@0: aoqi@0: eden_limit = align_size_down(eden_limit, _space_alignment); aoqi@0: aoqi@0: // And one last limit check, now that we've aligned things. aoqi@0: if (desired_eden_size > eden_limit) { aoqi@0: // If the policy says to get a larger eden but aoqi@0: // is hitting the limit, don't decrease eden. aoqi@0: // This can lead to a general drifting down of the aoqi@0: // eden size. Let the tenuring calculation push more aoqi@0: // into the old gen. aoqi@0: desired_eden_size = MAX2(eden_limit, cur_eden); aoqi@0: } aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: // Timing stats aoqi@0: gclog_or_tty->print( aoqi@0: "PSAdaptiveSizePolicy::compute_eden_space_size: costs" aoqi@0: " minor_time: %f" aoqi@0: " major_cost: %f" aoqi@0: " mutator_cost: %f" aoqi@0: " throughput_goal: %f", aoqi@0: minor_gc_cost(), major_gc_cost(), mutator_cost(), aoqi@0: _throughput_goal); aoqi@0: aoqi@0: // We give more details if Verbose is set aoqi@0: if (Verbose) { aoqi@0: gclog_or_tty->print( " minor_pause: %f" aoqi@0: " major_pause: %f" aoqi@0: " minor_interval: %f" aoqi@0: " major_interval: %f" aoqi@0: " pause_goal: %f", aoqi@0: _avg_minor_pause->padded_average(), aoqi@0: _avg_major_pause->padded_average(), aoqi@0: _avg_minor_interval->average(), aoqi@0: _avg_major_interval->average(), aoqi@0: gc_pause_goal_sec()); aoqi@0: } aoqi@0: aoqi@0: // Footprint stats aoqi@0: gclog_or_tty->print( " live_space: " SIZE_FORMAT aoqi@0: " free_space: " SIZE_FORMAT, aoqi@0: live_space(), free_space()); aoqi@0: // More detail aoqi@0: if (Verbose) { aoqi@0: gclog_or_tty->print( " base_footprint: " SIZE_FORMAT aoqi@0: " avg_young_live: " SIZE_FORMAT aoqi@0: " avg_old_live: " SIZE_FORMAT, aoqi@0: (size_t)_avg_base_footprint->average(), aoqi@0: (size_t)avg_young_live()->average(), aoqi@0: (size_t)avg_old_live()->average()); aoqi@0: } aoqi@0: aoqi@0: // And finally, our old and new sizes. aoqi@0: gclog_or_tty->print(" old_eden_size: " SIZE_FORMAT aoqi@0: " desired_eden_size: " SIZE_FORMAT, aoqi@0: _eden_size, desired_eden_size); aoqi@0: gclog_or_tty->cr(); aoqi@0: } aoqi@0: aoqi@0: set_eden_size(desired_eden_size); aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::compute_old_gen_free_space( aoqi@0: size_t old_live, aoqi@0: size_t cur_eden, aoqi@0: size_t max_old_gen_size, aoqi@0: bool is_full_gc) { aoqi@0: aoqi@0: // Update statistics aoqi@0: // Time statistics are updated as we go, update footprint stats here aoqi@0: if (is_full_gc) { aoqi@0: // old_live is only accurate after a full gc aoqi@0: avg_old_live()->sample(old_live); aoqi@0: } aoqi@0: aoqi@0: // This code used to return if the policy was not ready , i.e., aoqi@0: // policy_is_ready() returning false. The intent was that aoqi@0: // decisions below needed major collection times and so could aoqi@0: // not be made before two major collections. A consequence was aoqi@0: // adjustments to the young generation were not done until after aoqi@0: // two major collections even if the minor collections times aoqi@0: // exceeded the requested goals. Now let the young generation aoqi@0: // adjust for the minor collection times. Major collection times aoqi@0: // will be zero for the first collection and will naturally be aoqi@0: // ignored. Tenured generation adjustments are only made at the aoqi@0: // full collections so until the second major collection has aoqi@0: // been reached, no tenured generation adjustments will be made. aoqi@0: aoqi@0: // Until we know better, desired promotion size uses the last calculation aoqi@0: size_t desired_promo_size = _promo_size; aoqi@0: aoqi@0: // Start eden at the current value. The desired value that is stored aoqi@0: // in _eden_size is not bounded by constraints of the heap and can aoqi@0: // run away. aoqi@0: // aoqi@0: // As expected setting desired_eden_size to the current aoqi@0: // value of desired_eden_size as a starting point aoqi@0: // caused desired_eden_size to grow way too large and caused aoqi@0: // an overflow down stream. It may have improved performance in aoqi@0: // some case but is dangerous. aoqi@0: size_t desired_eden_size = cur_eden; aoqi@0: aoqi@0: // Cache some values. There's a bit of work getting these, so aoqi@0: // we might save a little time. aoqi@0: const double major_cost = major_gc_cost(); aoqi@0: const double minor_cost = minor_gc_cost(); aoqi@0: aoqi@0: // Limits on our growth aoqi@0: size_t promo_limit = (size_t)(max_old_gen_size - avg_old_live()->average()); aoqi@0: aoqi@0: // But don't force a promo size below the current promo size. Otherwise, aoqi@0: // the promo size will shrink for no good reason. aoqi@0: promo_limit = MAX2(promo_limit, _promo_size); aoqi@0: aoqi@0: const double gc_cost_limit = GCTimeLimit/100.0; aoqi@0: aoqi@0: // Which way should we go? aoqi@0: // if pause requirement is not met aoqi@0: // adjust size of any generation with average paus exceeding aoqi@0: // the pause limit. Adjust one pause at a time (the larger) aoqi@0: // and only make adjustments for the major pause at full collections. aoqi@0: // else if throughput requirement not met aoqi@0: // adjust the size of the generation with larger gc time. Only aoqi@0: // adjust one generation at a time. aoqi@0: // else aoqi@0: // adjust down the total heap size. Adjust down the larger of the aoqi@0: // generations. aoqi@0: aoqi@0: // Add some checks for a threshhold for a change. For example, aoqi@0: // a change less than the necessary alignment is probably not worth aoqi@0: // attempting. aoqi@0: aoqi@0: if ((_avg_minor_pause->padded_average() > gc_pause_goal_sec()) || aoqi@0: (_avg_major_pause->padded_average() > gc_pause_goal_sec())) { aoqi@0: // aoqi@0: // Check pauses aoqi@0: // aoqi@0: // Make changes only to affect one of the pauses (the larger) aoqi@0: // at a time. aoqi@0: if (is_full_gc) { aoqi@0: set_decide_at_full_gc(decide_at_full_gc_true); aoqi@0: adjust_promo_for_pause_time(is_full_gc, &desired_promo_size, &desired_eden_size); aoqi@0: } aoqi@0: } else if (_avg_minor_pause->padded_average() > gc_minor_pause_goal_sec()) { aoqi@0: // Adjust only for the minor pause time goal aoqi@0: adjust_promo_for_minor_pause_time(is_full_gc, &desired_promo_size, &desired_eden_size); aoqi@0: } else if(adjusted_mutator_cost() < _throughput_goal) { aoqi@0: // This branch used to require that (mutator_cost() > 0.0 in 1.4.2. aoqi@0: // This sometimes resulted in skipping to the minimize footprint aoqi@0: // code. Change this to try and reduce GC time if mutator time is aoqi@0: // negative for whatever reason. Or for future consideration, aoqi@0: // bail out of the code if mutator time is negative. aoqi@0: // aoqi@0: // Throughput aoqi@0: // aoqi@0: assert(major_cost >= 0.0, "major cost is < 0.0"); aoqi@0: assert(minor_cost >= 0.0, "minor cost is < 0.0"); aoqi@0: // Try to reduce the GC times. aoqi@0: if (is_full_gc) { aoqi@0: set_decide_at_full_gc(decide_at_full_gc_true); aoqi@0: adjust_promo_for_throughput(is_full_gc, &desired_promo_size); aoqi@0: } aoqi@0: } else { aoqi@0: aoqi@0: // Be conservative about reducing the footprint. aoqi@0: // Do a minimum number of major collections first. aoqi@0: // Have reasonable averages for major and minor collections costs. aoqi@0: if (UseAdaptiveSizePolicyFootprintGoal && aoqi@0: young_gen_policy_is_ready() && aoqi@0: avg_major_gc_cost()->average() >= 0.0 && aoqi@0: avg_minor_gc_cost()->average() >= 0.0) { aoqi@0: if (is_full_gc) { aoqi@0: set_decide_at_full_gc(decide_at_full_gc_true); aoqi@0: size_t desired_sum = desired_eden_size + desired_promo_size; aoqi@0: desired_promo_size = adjust_promo_for_footprint(desired_promo_size, desired_sum); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Note we make the same tests as in the code block below; the code aoqi@0: // seems a little easier to read with the printing in another block. aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: if (desired_promo_size > promo_limit) { aoqi@0: // "free_in_old_gen" was the original value for used for promo_limit aoqi@0: size_t free_in_old_gen = (size_t)(max_old_gen_size - avg_old_live()->average()); aoqi@0: gclog_or_tty->print_cr( aoqi@0: "PSAdaptiveSizePolicy::compute_old_gen_free_space limits:" aoqi@0: " desired_promo_size: " SIZE_FORMAT aoqi@0: " promo_limit: " SIZE_FORMAT aoqi@0: " free_in_old_gen: " SIZE_FORMAT aoqi@0: " max_old_gen_size: " SIZE_FORMAT aoqi@0: " avg_old_live: " SIZE_FORMAT, aoqi@0: desired_promo_size, promo_limit, free_in_old_gen, aoqi@0: max_old_gen_size, (size_t) avg_old_live()->average()); aoqi@0: } aoqi@0: if (gc_cost() > gc_cost_limit) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "PSAdaptiveSizePolicy::compute_old_gen_free_space: gc time limit" aoqi@0: " gc_cost: %f " aoqi@0: " GCTimeLimit: %d", aoqi@0: gc_cost(), GCTimeLimit); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Align everything and make a final limit check aoqi@0: desired_promo_size = align_size_up(desired_promo_size, _space_alignment); aoqi@0: desired_promo_size = MAX2(desired_promo_size, _space_alignment); aoqi@0: aoqi@0: promo_limit = align_size_down(promo_limit, _space_alignment); aoqi@0: aoqi@0: // And one last limit check, now that we've aligned things. aoqi@0: desired_promo_size = MIN2(desired_promo_size, promo_limit); aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: // Timing stats aoqi@0: gclog_or_tty->print( aoqi@0: "PSAdaptiveSizePolicy::compute_old_gen_free_space: costs" aoqi@0: " minor_time: %f" aoqi@0: " major_cost: %f" aoqi@0: " mutator_cost: %f" aoqi@0: " throughput_goal: %f", aoqi@0: minor_gc_cost(), major_gc_cost(), mutator_cost(), aoqi@0: _throughput_goal); aoqi@0: aoqi@0: // We give more details if Verbose is set aoqi@0: if (Verbose) { aoqi@0: gclog_or_tty->print( " minor_pause: %f" aoqi@0: " major_pause: %f" aoqi@0: " minor_interval: %f" aoqi@0: " major_interval: %f" aoqi@0: " pause_goal: %f", aoqi@0: _avg_minor_pause->padded_average(), aoqi@0: _avg_major_pause->padded_average(), aoqi@0: _avg_minor_interval->average(), aoqi@0: _avg_major_interval->average(), aoqi@0: gc_pause_goal_sec()); aoqi@0: } aoqi@0: aoqi@0: // Footprint stats aoqi@0: gclog_or_tty->print( " live_space: " SIZE_FORMAT aoqi@0: " free_space: " SIZE_FORMAT, aoqi@0: live_space(), free_space()); aoqi@0: // More detail aoqi@0: if (Verbose) { aoqi@0: gclog_or_tty->print( " base_footprint: " SIZE_FORMAT aoqi@0: " avg_young_live: " SIZE_FORMAT aoqi@0: " avg_old_live: " SIZE_FORMAT, aoqi@0: (size_t)_avg_base_footprint->average(), aoqi@0: (size_t)avg_young_live()->average(), aoqi@0: (size_t)avg_old_live()->average()); aoqi@0: } aoqi@0: aoqi@0: // And finally, our old and new sizes. aoqi@0: gclog_or_tty->print(" old_promo_size: " SIZE_FORMAT aoqi@0: " desired_promo_size: " SIZE_FORMAT, aoqi@0: _promo_size, desired_promo_size); aoqi@0: gclog_or_tty->cr(); aoqi@0: } aoqi@0: aoqi@0: set_promo_size(desired_promo_size); aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::decay_supplemental_growth(bool is_full_gc) { aoqi@0: // Decay the supplemental increment? Decay the supplement growth aoqi@0: // factor even if it is not used. It is only meant to give a boost aoqi@0: // to the initial growth and if it is not used, then it was not aoqi@0: // needed. aoqi@0: if (is_full_gc) { aoqi@0: // Don't wait for the threshold value for the major collections. If aoqi@0: // here, the supplemental growth term was used and should decay. aoqi@0: if ((_avg_major_pause->count() % TenuredGenerationSizeSupplementDecay) aoqi@0: == 0) { aoqi@0: _old_gen_size_increment_supplement = aoqi@0: _old_gen_size_increment_supplement >> 1; aoqi@0: } aoqi@0: } else { aoqi@0: if ((_avg_minor_pause->count() >= AdaptiveSizePolicyReadyThreshold) && aoqi@0: (_avg_minor_pause->count() % YoungGenerationSizeSupplementDecay) == 0) { aoqi@0: _young_gen_size_increment_supplement = aoqi@0: _young_gen_size_increment_supplement >> 1; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::adjust_promo_for_minor_pause_time(bool is_full_gc, aoqi@0: size_t* desired_promo_size_ptr, size_t* desired_eden_size_ptr) { aoqi@0: aoqi@0: if (PSAdjustTenuredGenForMinorPause) { aoqi@0: if (is_full_gc) { aoqi@0: set_decide_at_full_gc(decide_at_full_gc_true); aoqi@0: } aoqi@0: // If the desired eden size is as small as it will get, aoqi@0: // try to adjust the old gen size. aoqi@0: if (*desired_eden_size_ptr <= _space_alignment) { aoqi@0: // Vary the old gen size to reduce the young gen pause. This aoqi@0: // may not be a good idea. This is just a test. aoqi@0: if (minor_pause_old_estimator()->decrement_will_decrease()) { aoqi@0: set_change_old_gen_for_min_pauses(decrease_old_gen_for_min_pauses_true); aoqi@0: *desired_promo_size_ptr = aoqi@0: _promo_size - promo_decrement_aligned_down(*desired_promo_size_ptr); aoqi@0: } else { aoqi@0: set_change_old_gen_for_min_pauses(increase_old_gen_for_min_pauses_true); aoqi@0: size_t promo_heap_delta = aoqi@0: promo_increment_with_supplement_aligned_up(*desired_promo_size_ptr); aoqi@0: if ((*desired_promo_size_ptr + promo_heap_delta) > aoqi@0: *desired_promo_size_ptr) { aoqi@0: *desired_promo_size_ptr = aoqi@0: _promo_size + promo_heap_delta; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::adjust_eden_for_minor_pause_time(bool is_full_gc, aoqi@0: size_t* desired_eden_size_ptr) { aoqi@0: aoqi@0: // Adjust the young generation size to reduce pause time of aoqi@0: // of collections. aoqi@0: // aoqi@0: // The AdaptiveSizePolicyInitializingSteps test is not used aoqi@0: // here. It has not seemed to be needed but perhaps should aoqi@0: // be added for consistency. aoqi@0: if (minor_pause_young_estimator()->decrement_will_decrease()) { aoqi@0: // reduce eden size aoqi@0: set_change_young_gen_for_min_pauses( aoqi@0: decrease_young_gen_for_min_pauses_true); aoqi@0: *desired_eden_size_ptr = *desired_eden_size_ptr - aoqi@0: eden_decrement_aligned_down(*desired_eden_size_ptr); aoqi@0: } else { aoqi@0: // EXPERIMENTAL ADJUSTMENT aoqi@0: // Only record that the estimator indicated such an action. aoqi@0: // *desired_eden_size_ptr = *desired_eden_size_ptr + eden_heap_delta; aoqi@0: set_change_young_gen_for_min_pauses( aoqi@0: increase_young_gen_for_min_pauses_true); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::adjust_promo_for_pause_time(bool is_full_gc, aoqi@0: size_t* desired_promo_size_ptr, aoqi@0: size_t* desired_eden_size_ptr) { aoqi@0: aoqi@0: size_t promo_heap_delta = 0; aoqi@0: // Add some checks for a threshold for a change. For example, aoqi@0: // a change less than the required alignment is probably not worth aoqi@0: // attempting. aoqi@0: aoqi@0: if (_avg_minor_pause->padded_average() > _avg_major_pause->padded_average()) { aoqi@0: adjust_promo_for_minor_pause_time(is_full_gc, desired_promo_size_ptr, desired_eden_size_ptr); aoqi@0: // major pause adjustments aoqi@0: } else if (is_full_gc) { aoqi@0: // Adjust for the major pause time only at full gc's because the aoqi@0: // affects of a change can only be seen at full gc's. aoqi@0: aoqi@0: // Reduce old generation size to reduce pause? aoqi@0: if (major_pause_old_estimator()->decrement_will_decrease()) { aoqi@0: // reduce old generation size aoqi@0: set_change_old_gen_for_maj_pauses(decrease_old_gen_for_maj_pauses_true); aoqi@0: promo_heap_delta = promo_decrement_aligned_down(*desired_promo_size_ptr); aoqi@0: *desired_promo_size_ptr = _promo_size - promo_heap_delta; aoqi@0: } else { aoqi@0: // EXPERIMENTAL ADJUSTMENT aoqi@0: // Only record that the estimator indicated such an action. aoqi@0: // *desired_promo_size_ptr = _promo_size + aoqi@0: // promo_increment_aligned_up(*desired_promo_size_ptr); aoqi@0: set_change_old_gen_for_maj_pauses(increase_old_gen_for_maj_pauses_true); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "PSAdaptiveSizePolicy::adjust_promo_for_pause_time " aoqi@0: "adjusting gen sizes for major pause (avg %f goal %f). " aoqi@0: "desired_promo_size " SIZE_FORMAT " promo delta " SIZE_FORMAT, aoqi@0: _avg_major_pause->average(), gc_pause_goal_sec(), aoqi@0: *desired_promo_size_ptr, promo_heap_delta); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::adjust_eden_for_pause_time(bool is_full_gc, aoqi@0: size_t* desired_promo_size_ptr, aoqi@0: size_t* desired_eden_size_ptr) { aoqi@0: aoqi@0: size_t eden_heap_delta = 0; aoqi@0: // Add some checks for a threshold for a change. For example, aoqi@0: // a change less than the required alignment is probably not worth aoqi@0: // attempting. aoqi@0: if (_avg_minor_pause->padded_average() > _avg_major_pause->padded_average()) { aoqi@0: adjust_eden_for_minor_pause_time(is_full_gc, aoqi@0: desired_eden_size_ptr); aoqi@0: // major pause adjustments aoqi@0: } else if (is_full_gc) { aoqi@0: // Adjust for the major pause time only at full gc's because the aoqi@0: // affects of a change can only be seen at full gc's. aoqi@0: if (PSAdjustYoungGenForMajorPause) { aoqi@0: // If the promo size is at the minimum (i.e., the old gen aoqi@0: // size will not actually decrease), consider changing the aoqi@0: // young gen size. aoqi@0: if (*desired_promo_size_ptr < _space_alignment) { aoqi@0: // If increasing the young generation will decrease the old gen aoqi@0: // pause, do it. aoqi@0: // During startup there is noise in the statistics for deciding aoqi@0: // on whether to increase or decrease the young gen size. For aoqi@0: // some number of iterations, just try to increase the young aoqi@0: // gen size if the major pause is too long to try and establish aoqi@0: // good statistics for later decisions. aoqi@0: if (major_pause_young_estimator()->increment_will_decrease() || aoqi@0: (_young_gen_change_for_major_pause_count aoqi@0: <= AdaptiveSizePolicyInitializingSteps)) { aoqi@0: set_change_young_gen_for_maj_pauses( aoqi@0: increase_young_gen_for_maj_pauses_true); aoqi@0: eden_heap_delta = eden_increment_aligned_up(*desired_eden_size_ptr); aoqi@0: *desired_eden_size_ptr = _eden_size + eden_heap_delta; aoqi@0: _young_gen_change_for_major_pause_count++; aoqi@0: } else { aoqi@0: // Record that decreasing the young gen size would decrease aoqi@0: // the major pause aoqi@0: set_change_young_gen_for_maj_pauses( aoqi@0: decrease_young_gen_for_maj_pauses_true); aoqi@0: eden_heap_delta = eden_decrement_aligned_down(*desired_eden_size_ptr); aoqi@0: *desired_eden_size_ptr = _eden_size - eden_heap_delta; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "PSAdaptiveSizePolicy::adjust_eden_for_pause_time " aoqi@0: "adjusting gen sizes for major pause (avg %f goal %f). " aoqi@0: "desired_eden_size " SIZE_FORMAT " eden delta " SIZE_FORMAT, aoqi@0: _avg_major_pause->average(), gc_pause_goal_sec(), aoqi@0: *desired_eden_size_ptr, eden_heap_delta); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::adjust_promo_for_throughput(bool is_full_gc, aoqi@0: size_t* desired_promo_size_ptr) { aoqi@0: aoqi@0: // Add some checks for a threshold for a change. For example, aoqi@0: // a change less than the required alignment is probably not worth aoqi@0: // attempting. aoqi@0: aoqi@0: if ((gc_cost() + mutator_cost()) == 0.0) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print("\nPSAdaptiveSizePolicy::adjust_promo_for_throughput(" aoqi@0: "is_full: %d, promo: " SIZE_FORMAT "): ", aoqi@0: is_full_gc, *desired_promo_size_ptr); aoqi@0: gclog_or_tty->print_cr("mutator_cost %f major_gc_cost %f " aoqi@0: "minor_gc_cost %f", mutator_cost(), major_gc_cost(), minor_gc_cost()); aoqi@0: } aoqi@0: aoqi@0: // Tenured generation aoqi@0: if (is_full_gc) { aoqi@0: // Calculate the change to use for the tenured gen. aoqi@0: size_t scaled_promo_heap_delta = 0; aoqi@0: // Can the increment to the generation be scaled? aoqi@0: if (gc_cost() >= 0.0 && major_gc_cost() >= 0.0) { aoqi@0: size_t promo_heap_delta = aoqi@0: promo_increment_with_supplement_aligned_up(*desired_promo_size_ptr); aoqi@0: double scale_by_ratio = major_gc_cost() / gc_cost(); aoqi@0: scaled_promo_heap_delta = aoqi@0: (size_t) (scale_by_ratio * (double) promo_heap_delta); aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "Scaled tenured increment: " SIZE_FORMAT " by %f down to " aoqi@0: SIZE_FORMAT, aoqi@0: promo_heap_delta, scale_by_ratio, scaled_promo_heap_delta); aoqi@0: } aoqi@0: } else if (major_gc_cost() >= 0.0) { aoqi@0: // Scaling is not going to work. If the major gc time is the aoqi@0: // larger, give it a full increment. aoqi@0: if (major_gc_cost() >= minor_gc_cost()) { aoqi@0: scaled_promo_heap_delta = aoqi@0: promo_increment_with_supplement_aligned_up(*desired_promo_size_ptr); aoqi@0: } aoqi@0: } else { aoqi@0: // Don't expect to get here but it's ok if it does aoqi@0: // in the product build since the delta will be 0 aoqi@0: // and nothing will change. aoqi@0: assert(false, "Unexpected value for gc costs"); aoqi@0: } aoqi@0: aoqi@0: switch (AdaptiveSizeThroughPutPolicy) { aoqi@0: case 1: aoqi@0: // Early in the run the statistics might not be good. Until aoqi@0: // a specific number of collections have been, use the heuristic aoqi@0: // that a larger generation size means lower collection costs. aoqi@0: if (major_collection_estimator()->increment_will_decrease() || aoqi@0: (_old_gen_change_for_major_throughput aoqi@0: <= AdaptiveSizePolicyInitializingSteps)) { aoqi@0: // Increase tenured generation size to reduce major collection cost aoqi@0: if ((*desired_promo_size_ptr + scaled_promo_heap_delta) > aoqi@0: *desired_promo_size_ptr) { aoqi@0: *desired_promo_size_ptr = _promo_size + scaled_promo_heap_delta; aoqi@0: } aoqi@0: set_change_old_gen_for_throughput( aoqi@0: increase_old_gen_for_throughput_true); aoqi@0: _old_gen_change_for_major_throughput++; aoqi@0: } else { aoqi@0: // EXPERIMENTAL ADJUSTMENT aoqi@0: // Record that decreasing the old gen size would decrease aoqi@0: // the major collection cost but don't do it. aoqi@0: // *desired_promo_size_ptr = _promo_size - aoqi@0: // promo_decrement_aligned_down(*desired_promo_size_ptr); aoqi@0: set_change_old_gen_for_throughput( aoqi@0: decrease_old_gen_for_throughput_true); aoqi@0: } aoqi@0: aoqi@0: break; aoqi@0: default: aoqi@0: // Simplest strategy aoqi@0: if ((*desired_promo_size_ptr + scaled_promo_heap_delta) > aoqi@0: *desired_promo_size_ptr) { aoqi@0: *desired_promo_size_ptr = *desired_promo_size_ptr + aoqi@0: scaled_promo_heap_delta; aoqi@0: } aoqi@0: set_change_old_gen_for_throughput( aoqi@0: increase_old_gen_for_throughput_true); aoqi@0: _old_gen_change_for_major_throughput++; aoqi@0: } aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "adjusting tenured gen for throughput (avg %f goal %f). " aoqi@0: "desired_promo_size " SIZE_FORMAT " promo_delta " SIZE_FORMAT , aoqi@0: mutator_cost(), _throughput_goal, aoqi@0: *desired_promo_size_ptr, scaled_promo_heap_delta); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::adjust_eden_for_throughput(bool is_full_gc, aoqi@0: size_t* desired_eden_size_ptr) { aoqi@0: aoqi@0: // Add some checks for a threshold for a change. For example, aoqi@0: // a change less than the required alignment is probably not worth aoqi@0: // attempting. aoqi@0: aoqi@0: if ((gc_cost() + mutator_cost()) == 0.0) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print("\nPSAdaptiveSizePolicy::adjust_eden_for_throughput(" aoqi@0: "is_full: %d, cur_eden: " SIZE_FORMAT "): ", aoqi@0: is_full_gc, *desired_eden_size_ptr); aoqi@0: gclog_or_tty->print_cr("mutator_cost %f major_gc_cost %f " aoqi@0: "minor_gc_cost %f", mutator_cost(), major_gc_cost(), minor_gc_cost()); aoqi@0: } aoqi@0: aoqi@0: // Young generation aoqi@0: size_t scaled_eden_heap_delta = 0; aoqi@0: // Can the increment to the generation be scaled? aoqi@0: if (gc_cost() >= 0.0 && minor_gc_cost() >= 0.0) { aoqi@0: size_t eden_heap_delta = aoqi@0: eden_increment_with_supplement_aligned_up(*desired_eden_size_ptr); aoqi@0: double scale_by_ratio = minor_gc_cost() / gc_cost(); aoqi@0: assert(scale_by_ratio <= 1.0 && scale_by_ratio >= 0.0, "Scaling is wrong"); aoqi@0: scaled_eden_heap_delta = aoqi@0: (size_t) (scale_by_ratio * (double) eden_heap_delta); aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "Scaled eden increment: " SIZE_FORMAT " by %f down to " aoqi@0: SIZE_FORMAT, aoqi@0: eden_heap_delta, scale_by_ratio, scaled_eden_heap_delta); aoqi@0: } aoqi@0: } else if (minor_gc_cost() >= 0.0) { aoqi@0: // Scaling is not going to work. If the minor gc time is the aoqi@0: // larger, give it a full increment. aoqi@0: if (minor_gc_cost() > major_gc_cost()) { aoqi@0: scaled_eden_heap_delta = aoqi@0: eden_increment_with_supplement_aligned_up(*desired_eden_size_ptr); aoqi@0: } aoqi@0: } else { aoqi@0: // Don't expect to get here but it's ok if it does aoqi@0: // in the product build since the delta will be 0 aoqi@0: // and nothing will change. aoqi@0: assert(false, "Unexpected value for gc costs"); aoqi@0: } aoqi@0: aoqi@0: // Use a heuristic for some number of collections to give aoqi@0: // the averages time to settle down. aoqi@0: switch (AdaptiveSizeThroughPutPolicy) { aoqi@0: case 1: aoqi@0: if (minor_collection_estimator()->increment_will_decrease() || aoqi@0: (_young_gen_change_for_minor_throughput aoqi@0: <= AdaptiveSizePolicyInitializingSteps)) { aoqi@0: // Expand young generation size to reduce frequency of aoqi@0: // of collections. aoqi@0: if ((*desired_eden_size_ptr + scaled_eden_heap_delta) > aoqi@0: *desired_eden_size_ptr) { aoqi@0: *desired_eden_size_ptr = aoqi@0: *desired_eden_size_ptr + scaled_eden_heap_delta; aoqi@0: } aoqi@0: set_change_young_gen_for_throughput( aoqi@0: increase_young_gen_for_througput_true); aoqi@0: _young_gen_change_for_minor_throughput++; aoqi@0: } else { aoqi@0: // EXPERIMENTAL ADJUSTMENT aoqi@0: // Record that decreasing the young gen size would decrease aoqi@0: // the minor collection cost but don't do it. aoqi@0: // *desired_eden_size_ptr = _eden_size - aoqi@0: // eden_decrement_aligned_down(*desired_eden_size_ptr); aoqi@0: set_change_young_gen_for_throughput( aoqi@0: decrease_young_gen_for_througput_true); aoqi@0: } aoqi@0: break; aoqi@0: default: aoqi@0: if ((*desired_eden_size_ptr + scaled_eden_heap_delta) > aoqi@0: *desired_eden_size_ptr) { aoqi@0: *desired_eden_size_ptr = aoqi@0: *desired_eden_size_ptr + scaled_eden_heap_delta; aoqi@0: } aoqi@0: set_change_young_gen_for_throughput( aoqi@0: increase_young_gen_for_througput_true); aoqi@0: _young_gen_change_for_minor_throughput++; aoqi@0: } aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "adjusting eden for throughput (avg %f goal %f). desired_eden_size " aoqi@0: SIZE_FORMAT " eden delta " SIZE_FORMAT "\n", aoqi@0: mutator_cost(), _throughput_goal, aoqi@0: *desired_eden_size_ptr, scaled_eden_heap_delta); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::adjust_promo_for_footprint( aoqi@0: size_t desired_promo_size, size_t desired_sum) { aoqi@0: assert(desired_promo_size <= desired_sum, "Inconsistent parameters"); aoqi@0: set_decrease_for_footprint(decrease_old_gen_for_footprint_true); aoqi@0: aoqi@0: size_t change = promo_decrement(desired_promo_size); aoqi@0: change = scale_down(change, desired_promo_size, desired_sum); aoqi@0: aoqi@0: size_t reduced_size = desired_promo_size - change; aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "AdaptiveSizePolicy::adjust_promo_for_footprint " aoqi@0: "adjusting tenured gen for footprint. " aoqi@0: "starting promo size " SIZE_FORMAT aoqi@0: " reduced promo size " SIZE_FORMAT aoqi@0: " promo delta " SIZE_FORMAT, aoqi@0: desired_promo_size, reduced_size, change ); aoqi@0: } aoqi@0: aoqi@0: assert(reduced_size <= desired_promo_size, "Inconsistent result"); aoqi@0: return reduced_size; aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::adjust_eden_for_footprint( aoqi@0: size_t desired_eden_size, size_t desired_sum) { aoqi@0: assert(desired_eden_size <= desired_sum, "Inconsistent parameters"); aoqi@0: set_decrease_for_footprint(decrease_young_gen_for_footprint_true); aoqi@0: aoqi@0: size_t change = eden_decrement(desired_eden_size); aoqi@0: change = scale_down(change, desired_eden_size, desired_sum); aoqi@0: aoqi@0: size_t reduced_size = desired_eden_size - change; aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "AdaptiveSizePolicy::adjust_eden_for_footprint " aoqi@0: "adjusting eden for footprint. " aoqi@0: " starting eden size " SIZE_FORMAT aoqi@0: " reduced eden size " SIZE_FORMAT aoqi@0: " eden delta " SIZE_FORMAT, aoqi@0: desired_eden_size, reduced_size, change); aoqi@0: } aoqi@0: aoqi@0: assert(reduced_size <= desired_eden_size, "Inconsistent result"); aoqi@0: return reduced_size; aoqi@0: } aoqi@0: aoqi@0: // Scale down "change" by the factor aoqi@0: // part / total aoqi@0: // Don't align the results. aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::scale_down(size_t change, aoqi@0: double part, aoqi@0: double total) { aoqi@0: assert(part <= total, "Inconsistent input"); aoqi@0: size_t reduced_change = change; aoqi@0: if (total > 0) { aoqi@0: double fraction = part / total; aoqi@0: reduced_change = (size_t) (fraction * (double) change); aoqi@0: } aoqi@0: assert(reduced_change <= change, "Inconsistent result"); aoqi@0: return reduced_change; aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::eden_increment(size_t cur_eden, aoqi@0: uint percent_change) { aoqi@0: size_t eden_heap_delta; aoqi@0: eden_heap_delta = cur_eden / 100 * percent_change; aoqi@0: return eden_heap_delta; aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::eden_increment(size_t cur_eden) { aoqi@0: return eden_increment(cur_eden, YoungGenerationSizeIncrement); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::eden_increment_aligned_up(size_t cur_eden) { aoqi@0: size_t result = eden_increment(cur_eden, YoungGenerationSizeIncrement); aoqi@0: return align_size_up(result, _space_alignment); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::eden_increment_aligned_down(size_t cur_eden) { aoqi@0: size_t result = eden_increment(cur_eden); aoqi@0: return align_size_down(result, _space_alignment); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::eden_increment_with_supplement_aligned_up( aoqi@0: size_t cur_eden) { aoqi@0: size_t result = eden_increment(cur_eden, aoqi@0: YoungGenerationSizeIncrement + _young_gen_size_increment_supplement); aoqi@0: return align_size_up(result, _space_alignment); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::eden_decrement_aligned_down(size_t cur_eden) { aoqi@0: size_t eden_heap_delta = eden_decrement(cur_eden); aoqi@0: return align_size_down(eden_heap_delta, _space_alignment); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::eden_decrement(size_t cur_eden) { aoqi@0: size_t eden_heap_delta = eden_increment(cur_eden) / aoqi@0: AdaptiveSizeDecrementScaleFactor; aoqi@0: return eden_heap_delta; aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::promo_increment(size_t cur_promo, aoqi@0: uint percent_change) { aoqi@0: size_t promo_heap_delta; aoqi@0: promo_heap_delta = cur_promo / 100 * percent_change; aoqi@0: return promo_heap_delta; aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::promo_increment(size_t cur_promo) { aoqi@0: return promo_increment(cur_promo, TenuredGenerationSizeIncrement); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::promo_increment_aligned_up(size_t cur_promo) { aoqi@0: size_t result = promo_increment(cur_promo, TenuredGenerationSizeIncrement); aoqi@0: return align_size_up(result, _space_alignment); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::promo_increment_aligned_down(size_t cur_promo) { aoqi@0: size_t result = promo_increment(cur_promo, TenuredGenerationSizeIncrement); aoqi@0: return align_size_down(result, _space_alignment); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::promo_increment_with_supplement_aligned_up( aoqi@0: size_t cur_promo) { aoqi@0: size_t result = promo_increment(cur_promo, aoqi@0: TenuredGenerationSizeIncrement + _old_gen_size_increment_supplement); aoqi@0: return align_size_up(result, _space_alignment); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::promo_decrement_aligned_down(size_t cur_promo) { aoqi@0: size_t promo_heap_delta = promo_decrement(cur_promo); aoqi@0: return align_size_down(promo_heap_delta, _space_alignment); aoqi@0: } aoqi@0: aoqi@0: size_t PSAdaptiveSizePolicy::promo_decrement(size_t cur_promo) { aoqi@0: size_t promo_heap_delta = promo_increment(cur_promo); aoqi@0: promo_heap_delta = promo_heap_delta / AdaptiveSizeDecrementScaleFactor; aoqi@0: return promo_heap_delta; aoqi@0: } aoqi@0: aoqi@0: uint PSAdaptiveSizePolicy::compute_survivor_space_size_and_threshold( aoqi@0: bool is_survivor_overflow, aoqi@0: uint tenuring_threshold, aoqi@0: size_t survivor_limit) { aoqi@0: assert(survivor_limit >= _space_alignment, aoqi@0: "survivor_limit too small"); aoqi@0: assert((size_t)align_size_down(survivor_limit, _space_alignment) aoqi@0: == survivor_limit, "survivor_limit not aligned"); aoqi@0: aoqi@0: // This method is called even if the tenuring threshold and survivor aoqi@0: // spaces are not adjusted so that the averages are sampled above. aoqi@0: if (!UsePSAdaptiveSurvivorSizePolicy || aoqi@0: !young_gen_policy_is_ready()) { aoqi@0: return tenuring_threshold; aoqi@0: } aoqi@0: aoqi@0: // We'll decide whether to increase or decrease the tenuring aoqi@0: // threshold based partly on the newly computed survivor size aoqi@0: // (if we hit the maximum limit allowed, we'll always choose to aoqi@0: // decrement the threshold). aoqi@0: bool incr_tenuring_threshold = false; aoqi@0: bool decr_tenuring_threshold = false; aoqi@0: aoqi@0: set_decrement_tenuring_threshold_for_gc_cost(false); aoqi@0: set_increment_tenuring_threshold_for_gc_cost(false); aoqi@0: set_decrement_tenuring_threshold_for_survivor_limit(false); aoqi@0: aoqi@0: if (!is_survivor_overflow) { aoqi@0: // Keep running averages on how much survived aoqi@0: aoqi@0: // We use the tenuring threshold to equalize the cost of major aoqi@0: // and minor collections. aoqi@0: // ThresholdTolerance is used to indicate how sensitive the aoqi@0: // tenuring threshold is to differences in cost betweent the aoqi@0: // collection types. aoqi@0: aoqi@0: // Get the times of interest. This involves a little work, so aoqi@0: // we cache the values here. aoqi@0: const double major_cost = major_gc_cost(); aoqi@0: const double minor_cost = minor_gc_cost(); aoqi@0: aoqi@0: if (minor_cost > major_cost * _threshold_tolerance_percent) { aoqi@0: // Minor times are getting too long; lower the threshold so aoqi@0: // less survives and more is promoted. aoqi@0: decr_tenuring_threshold = true; aoqi@0: set_decrement_tenuring_threshold_for_gc_cost(true); aoqi@0: } else if (major_cost > minor_cost * _threshold_tolerance_percent) { aoqi@0: // Major times are too long, so we want less promotion. aoqi@0: incr_tenuring_threshold = true; aoqi@0: set_increment_tenuring_threshold_for_gc_cost(true); aoqi@0: } aoqi@0: aoqi@0: } else { aoqi@0: // Survivor space overflow occurred, so promoted and survived are aoqi@0: // not accurate. We'll make our best guess by combining survived aoqi@0: // and promoted and count them as survivors. aoqi@0: // aoqi@0: // We'll lower the tenuring threshold to see if we can correct aoqi@0: // things. Also, set the survivor size conservatively. We're aoqi@0: // trying to avoid many overflows from occurring if defnew size aoqi@0: // is just too small. aoqi@0: aoqi@0: decr_tenuring_threshold = true; aoqi@0: } aoqi@0: aoqi@0: // The padded average also maintains a deviation from the average; aoqi@0: // we use this to see how good of an estimate we have of what survived. aoqi@0: // We're trying to pad the survivor size as little as possible without aoqi@0: // overflowing the survivor spaces. aoqi@0: size_t target_size = align_size_up((size_t)_avg_survived->padded_average(), aoqi@0: _space_alignment); aoqi@0: target_size = MAX2(target_size, _space_alignment); aoqi@0: aoqi@0: if (target_size > survivor_limit) { aoqi@0: // Target size is bigger than we can handle. Let's also reduce aoqi@0: // the tenuring threshold. aoqi@0: target_size = survivor_limit; aoqi@0: decr_tenuring_threshold = true; aoqi@0: set_decrement_tenuring_threshold_for_survivor_limit(true); aoqi@0: } aoqi@0: aoqi@0: // Finally, increment or decrement the tenuring threshold, as decided above. aoqi@0: // We test for decrementing first, as we might have hit the target size aoqi@0: // limit. aoqi@0: if (decr_tenuring_threshold && !(AlwaysTenure || NeverTenure)) { aoqi@0: if (tenuring_threshold > 1) { aoqi@0: tenuring_threshold--; aoqi@0: } aoqi@0: } else if (incr_tenuring_threshold && !(AlwaysTenure || NeverTenure)) { aoqi@0: if (tenuring_threshold < MaxTenuringThreshold) { aoqi@0: tenuring_threshold++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // We keep a running average of the amount promoted which is used aoqi@0: // to decide when we should collect the old generation (when aoqi@0: // the amount of old gen free space is less than what we expect to aoqi@0: // promote). aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: // A little more detail if Verbose is on aoqi@0: if (Verbose) { aoqi@0: gclog_or_tty->print( " avg_survived: %f" aoqi@0: " avg_deviation: %f", aoqi@0: _avg_survived->average(), aoqi@0: _avg_survived->deviation()); aoqi@0: } aoqi@0: aoqi@0: gclog_or_tty->print( " avg_survived_padded_avg: %f", aoqi@0: _avg_survived->padded_average()); aoqi@0: aoqi@0: if (Verbose) { aoqi@0: gclog_or_tty->print( " avg_promoted_avg: %f" aoqi@0: " avg_promoted_dev: %f", aoqi@0: avg_promoted()->average(), aoqi@0: avg_promoted()->deviation()); aoqi@0: } aoqi@0: aoqi@0: gclog_or_tty->print_cr( " avg_promoted_padded_avg: %f" aoqi@0: " avg_pretenured_padded_avg: %f" aoqi@0: " tenuring_thresh: %d" aoqi@0: " target_size: " SIZE_FORMAT, aoqi@0: avg_promoted()->padded_average(), aoqi@0: _avg_pretenured->padded_average(), aoqi@0: tenuring_threshold, target_size); aoqi@0: } aoqi@0: aoqi@0: set_survivor_size(target_size); aoqi@0: aoqi@0: return tenuring_threshold; aoqi@0: } aoqi@0: aoqi@0: void PSAdaptiveSizePolicy::update_averages(bool is_survivor_overflow, aoqi@0: size_t survived, aoqi@0: size_t promoted) { aoqi@0: // Update averages aoqi@0: if (!is_survivor_overflow) { aoqi@0: // Keep running averages on how much survived aoqi@0: _avg_survived->sample(survived); aoqi@0: } else { aoqi@0: size_t survived_guess = survived + promoted; aoqi@0: _avg_survived->sample(survived_guess); aoqi@0: } aoqi@0: avg_promoted()->sample(promoted + _avg_pretenured->padded_average()); aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "AdaptiveSizePolicy::update_averages:" aoqi@0: " survived: " SIZE_FORMAT aoqi@0: " promoted: " SIZE_FORMAT aoqi@0: " overflow: %s", aoqi@0: survived, promoted, is_survivor_overflow ? "true" : "false"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool PSAdaptiveSizePolicy::print_adaptive_size_policy_on(outputStream* st) aoqi@0: const { aoqi@0: aoqi@0: if (!UseAdaptiveSizePolicy) return false; aoqi@0: aoqi@0: return AdaptiveSizePolicy::print_adaptive_size_policy_on( aoqi@0: st, aoqi@0: PSScavenge::tenuring_threshold()); aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: void TestOldFreeSpaceCalculation_test() { aoqi@0: assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(100, 20) == 25, "Calculation of free memory failed"); aoqi@0: assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(100, 50) == 100, "Calculation of free memory failed"); aoqi@0: assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(100, 60) == 150, "Calculation of free memory failed"); aoqi@0: assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(100, 75) == 300, "Calculation of free memory failed"); aoqi@0: assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(400, 20) == 100, "Calculation of free memory failed"); aoqi@0: assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(400, 50) == 400, "Calculation of free memory failed"); aoqi@0: assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(400, 60) == 600, "Calculation of free memory failed"); aoqi@0: assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(400, 75) == 1200, "Calculation of free memory failed"); aoqi@0: } aoqi@0: aoqi@0: #endif /* !PRODUCT */