1 /*
   2  * Copyright (c) 2002, 2019, 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/parallel/parallelScavengeHeap.hpp"
  27 #include "gc/parallel/psAdaptiveSizePolicy.hpp"
  28 #include "gc/parallel/psGCAdaptivePolicyCounters.hpp"
  29 #include "gc/parallel/psScavenge.hpp"
  30 #include "gc/shared/collectorPolicy.hpp"
  31 #include "gc/shared/gcCause.hpp"
  32 #include "gc/shared/gcUtil.inline.hpp"
  33 #include "gc/shared/gcPolicyCounters.hpp"
  34 #include "logging/log.hpp"
  35 #include "runtime/timer.hpp"
  36 #include "utilities/align.hpp"
  37 
  38 #include <math.h>
  39 
  40 PSAdaptiveSizePolicy::PSAdaptiveSizePolicy(size_t init_eden_size,
  41                                            size_t init_promo_size,
  42                                            size_t init_survivor_size,
  43                                            size_t space_alignment,
  44                                            double gc_pause_goal_sec,
  45                                            double gc_minor_pause_goal_sec,
  46                                            uint gc_cost_ratio) :
  47      AdaptiveSizePolicy(init_eden_size,
  48                         init_promo_size,
  49                         init_survivor_size,
  50                         gc_pause_goal_sec,
  51                         gc_cost_ratio),
  52      _avg_major_pause(new AdaptivePaddedAverage(AdaptiveTimeWeight, PausePadding)),
  53      _avg_base_footprint(new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight)),
  54      _gc_stats(),
  55      _collection_cost_margin_fraction(AdaptiveSizePolicyCollectionCostMargin / 100.0),
  56      _major_pause_old_estimator(new LinearLeastSquareFit(AdaptiveSizePolicyWeight)),
  57      _major_pause_young_estimator(new LinearLeastSquareFit(AdaptiveSizePolicyWeight)),
  58      _latest_major_mutator_interval_seconds(0),
  59      _space_alignment(space_alignment),
  60      _gc_minor_pause_goal_sec(gc_minor_pause_goal_sec),
  61      _live_at_last_full_gc(init_promo_size),
  62      _change_old_gen_for_min_pauses(0),
  63      _change_young_gen_for_maj_pauses(0),
  64      _old_gen_policy_is_ready(false),
  65      _young_gen_size_increment_supplement(YoungGenerationSizeSupplement),
  66      _old_gen_size_increment_supplement(TenuredGenerationSizeSupplement),
  67      _bytes_absorbed_from_eden(0)
  68 {
  69   // Start the timers
  70   _major_timer.start();
  71 }
  72 
  73 size_t PSAdaptiveSizePolicy::calculate_free_based_on_live(size_t live, uintx ratio_as_percentage) {
  74   // We want to calculate how much free memory there can be based on the
  75   // amount of live data currently in the old gen. Using the formula:
  76   // ratio * (free + live) = free
  77   // Some equation solving later we get:
  78   // free = (live * ratio) / (1 - ratio)
  79 
  80   const double ratio = ratio_as_percentage / 100.0;
  81   const double ratio_inverse = 1.0 - ratio;
  82   const double tmp = live * ratio;
  83   size_t free = (size_t)(tmp / ratio_inverse);
  84 
  85   return free;
  86 }
  87 
  88 size_t PSAdaptiveSizePolicy::calculated_old_free_size_in_bytes() const {
  89   size_t free_size = (size_t)(_promo_size + avg_promoted()->padded_average());
  90   size_t live = ParallelScavengeHeap::heap()->old_gen()->used_in_bytes();
  91 
  92   if (MinHeapFreeRatio != 0) {
  93     size_t min_free = calculate_free_based_on_live(live, MinHeapFreeRatio);
  94     free_size = MAX2(free_size, min_free);
  95   }
  96 
  97   if (MaxHeapFreeRatio != 100) {
  98     size_t max_free = calculate_free_based_on_live(live, MaxHeapFreeRatio);
  99     free_size = MIN2(max_free, free_size);
 100   }
 101 
 102   return free_size;
 103 }
 104 
 105 void PSAdaptiveSizePolicy::major_collection_begin() {
 106   // Update the interval time
 107   _major_timer.stop();
 108   // Save most recent collection time
 109   _latest_major_mutator_interval_seconds = _major_timer.seconds();
 110   _major_timer.reset();
 111   _major_timer.start();
 112 }
 113 
 114 void PSAdaptiveSizePolicy::update_minor_pause_old_estimator(
 115     double minor_pause_in_ms) {
 116   double promo_size_in_mbytes = ((double)_promo_size)/((double)M);
 117   _minor_pause_old_estimator->update(promo_size_in_mbytes,
 118     minor_pause_in_ms);
 119 }
 120 
 121 void PSAdaptiveSizePolicy::major_collection_end(size_t amount_live,
 122   GCCause::Cause gc_cause) {
 123   // Update the pause time.
 124   _major_timer.stop();
 125 
 126   if (should_update_promo_stats(gc_cause)) {
 127     double major_pause_in_seconds = _major_timer.seconds();
 128     double major_pause_in_ms = major_pause_in_seconds * MILLIUNITS;
 129 
 130     // Sample for performance counter
 131     _avg_major_pause->sample(major_pause_in_seconds);
 132 
 133     // Cost of collection (unit-less)
 134     double collection_cost = 0.0;
 135     if ((_latest_major_mutator_interval_seconds > 0.0) &&
 136         (major_pause_in_seconds > 0.0)) {
 137       double interval_in_seconds =
 138         _latest_major_mutator_interval_seconds + major_pause_in_seconds;
 139       collection_cost =
 140         major_pause_in_seconds / interval_in_seconds;
 141       avg_major_gc_cost()->sample(collection_cost);
 142 
 143       // Sample for performance counter
 144       _avg_major_interval->sample(interval_in_seconds);
 145     }
 146 
 147     // Calculate variables used to estimate pause time vs. gen sizes
 148     double eden_size_in_mbytes = ((double)_eden_size)/((double)M);
 149     double promo_size_in_mbytes = ((double)_promo_size)/((double)M);
 150     _major_pause_old_estimator->update(promo_size_in_mbytes,
 151       major_pause_in_ms);
 152     _major_pause_young_estimator->update(eden_size_in_mbytes,
 153       major_pause_in_ms);
 154 
 155     log_trace(gc, ergo)("psAdaptiveSizePolicy::major_collection_end: major gc cost: %f  average: %f",
 156                         collection_cost,avg_major_gc_cost()->average());
 157     log_trace(gc, ergo)("  major pause: %f major period %f",
 158                         major_pause_in_ms, _latest_major_mutator_interval_seconds * MILLIUNITS);
 159 
 160     // Calculate variable used to estimate collection cost vs. gen sizes
 161     assert(collection_cost >= 0.0, "Expected to be non-negative");
 162     _major_collection_estimator->update(promo_size_in_mbytes,
 163         collection_cost);
 164   }
 165 
 166   // Update the amount live at the end of a full GC
 167   _live_at_last_full_gc = amount_live;
 168 
 169   // The policy does not have enough data until at least some major collections
 170   // have been done.
 171   if (_avg_major_pause->count() >= AdaptiveSizePolicyReadyThreshold) {
 172     _old_gen_policy_is_ready = true;
 173   }
 174 
 175   // Interval times use this timer to measure the interval that
 176   // the mutator runs.  Reset after the GC pause has been measured.
 177   _major_timer.reset();
 178   _major_timer.start();
 179 }
 180 
 181 // If the remaining free space in the old generation is less that
 182 // that expected to be needed by the next collection, do a full
 183 // collection now.
 184 bool PSAdaptiveSizePolicy::should_full_GC(size_t old_free_in_bytes) {
 185 
 186   // A similar test is done in the scavenge's should_attempt_scavenge().  If
 187   // this is changed, decide if that test should also be changed.
 188   bool result = padded_average_promoted_in_bytes() > (float) old_free_in_bytes;
 189   log_trace(gc, ergo)("%s after scavenge average_promoted " SIZE_FORMAT " padded_average_promoted " SIZE_FORMAT " free in old gen " SIZE_FORMAT,
 190                       result ? "Full" : "No full",
 191                       (size_t) average_promoted_in_bytes(),
 192                       (size_t) padded_average_promoted_in_bytes(),
 193                       old_free_in_bytes);
 194   return result;
 195 }
 196 
 197 void PSAdaptiveSizePolicy::clear_generation_free_space_flags() {
 198 
 199   AdaptiveSizePolicy::clear_generation_free_space_flags();
 200 
 201   set_change_old_gen_for_min_pauses(0);
 202 
 203   set_change_young_gen_for_maj_pauses(0);
 204 }
 205 
 206 // If this is not a full GC, only test and modify the young generation.
 207 
 208 void PSAdaptiveSizePolicy::compute_generations_free_space(
 209                                            size_t young_live,
 210                                            size_t eden_live,
 211                                            size_t old_live,
 212                                            size_t cur_eden,
 213                                            size_t max_old_gen_size,
 214                                            size_t max_eden_size,
 215                                            bool   is_full_gc) {
 216   compute_eden_space_size(young_live,
 217                           eden_live,
 218                           cur_eden,
 219                           max_eden_size,
 220                           is_full_gc);
 221 
 222   compute_old_gen_free_space(old_live,
 223                              cur_eden,
 224                              max_old_gen_size,
 225                              is_full_gc);
 226 }
 227 
 228 void PSAdaptiveSizePolicy::compute_eden_space_size(
 229                                            size_t young_live,
 230                                            size_t eden_live,
 231                                            size_t cur_eden,
 232                                            size_t max_eden_size,
 233                                            bool   is_full_gc) {
 234 
 235   // Update statistics
 236   // Time statistics are updated as we go, update footprint stats here
 237   _avg_base_footprint->sample(BaseFootPrintEstimate);
 238   avg_young_live()->sample(young_live);
 239   avg_eden_live()->sample(eden_live);
 240 
 241   // This code used to return if the policy was not ready , i.e.,
 242   // policy_is_ready() returning false.  The intent was that
 243   // decisions below needed major collection times and so could
 244   // not be made before two major collections.  A consequence was
 245   // adjustments to the young generation were not done until after
 246   // two major collections even if the minor collections times
 247   // exceeded the requested goals.  Now let the young generation
 248   // adjust for the minor collection times.  Major collection times
 249   // will be zero for the first collection and will naturally be
 250   // ignored.  Tenured generation adjustments are only made at the
 251   // full collections so until the second major collection has
 252   // been reached, no tenured generation adjustments will be made.
 253 
 254   // Until we know better, desired promotion size uses the last calculation
 255   size_t desired_promo_size = _promo_size;
 256 
 257   // Start eden at the current value.  The desired value that is stored
 258   // in _eden_size is not bounded by constraints of the heap and can
 259   // run away.
 260   //
 261   // As expected setting desired_eden_size to the current
 262   // value of desired_eden_size as a starting point
 263   // caused desired_eden_size to grow way too large and caused
 264   // an overflow down stream.  It may have improved performance in
 265   // some case but is dangerous.
 266   size_t desired_eden_size = cur_eden;
 267 
 268   // Cache some values. There's a bit of work getting these, so
 269   // we might save a little time.
 270   const double major_cost = major_gc_cost();
 271   const double minor_cost = minor_gc_cost();
 272 
 273   // This method sets the desired eden size.  That plus the
 274   // desired survivor space sizes sets the desired young generation
 275   // size.  This methods does not know what the desired survivor
 276   // size is but expects that other policy will attempt to make
 277   // the survivor sizes compatible with the live data in the
 278   // young generation.  This limit is an estimate of the space left
 279   // in the young generation after the survivor spaces have been
 280   // subtracted out.
 281   size_t eden_limit = max_eden_size;
 282 
 283   const double gc_cost_limit = GCTimeLimit / 100.0;
 284 
 285   // Which way should we go?
 286   // if pause requirement is not met
 287   //   adjust size of any generation with average paus exceeding
 288   //   the pause limit.  Adjust one pause at a time (the larger)
 289   //   and only make adjustments for the major pause at full collections.
 290   // else if throughput requirement not met
 291   //   adjust the size of the generation with larger gc time.  Only
 292   //   adjust one generation at a time.
 293   // else
 294   //   adjust down the total heap size.  Adjust down the larger of the
 295   //   generations.
 296 
 297   // Add some checks for a threshold for a change.  For example,
 298   // a change less than the necessary alignment is probably not worth
 299   // attempting.
 300 
 301 
 302   if ((_avg_minor_pause->padded_average() > gc_pause_goal_sec()) ||
 303       (_avg_major_pause->padded_average() > gc_pause_goal_sec())) {
 304     //
 305     // Check pauses
 306     //
 307     // Make changes only to affect one of the pauses (the larger)
 308     // at a time.
 309     adjust_eden_for_pause_time(is_full_gc, &desired_promo_size, &desired_eden_size);
 310 
 311   } else if (_avg_minor_pause->padded_average() > gc_minor_pause_goal_sec()) {
 312     // Adjust only for the minor pause time goal
 313     adjust_eden_for_minor_pause_time(is_full_gc, &desired_eden_size);
 314 
 315   } else if(adjusted_mutator_cost() < _throughput_goal) {
 316     // This branch used to require that (mutator_cost() > 0.0 in 1.4.2.
 317     // This sometimes resulted in skipping to the minimize footprint
 318     // code.  Change this to try and reduce GC time if mutator time is
 319     // negative for whatever reason.  Or for future consideration,
 320     // bail out of the code if mutator time is negative.
 321     //
 322     // Throughput
 323     //
 324     assert(major_cost >= 0.0, "major cost is < 0.0");
 325     assert(minor_cost >= 0.0, "minor cost is < 0.0");
 326     // Try to reduce the GC times.
 327     adjust_eden_for_throughput(is_full_gc, &desired_eden_size);
 328 
 329   } else {
 330 
 331     // Be conservative about reducing the footprint.
 332     //   Do a minimum number of major collections first.
 333     //   Have reasonable averages for major and minor collections costs.
 334     if (UseAdaptiveSizePolicyFootprintGoal &&
 335         young_gen_policy_is_ready() &&
 336         avg_major_gc_cost()->average() >= 0.0 &&
 337         avg_minor_gc_cost()->average() >= 0.0) {
 338       size_t desired_sum = desired_eden_size + desired_promo_size;
 339       desired_eden_size = adjust_eden_for_footprint(desired_eden_size, desired_sum);
 340     }
 341   }
 342 
 343   // Note we make the same tests as in the code block below;  the code
 344   // seems a little easier to read with the printing in another block.
 345   if (desired_eden_size > eden_limit) {
 346     log_debug(gc, ergo)(
 347           "PSAdaptiveSizePolicy::compute_eden_space_size limits:"
 348           " desired_eden_size: " SIZE_FORMAT
 349           " old_eden_size: " SIZE_FORMAT
 350           " eden_limit: " SIZE_FORMAT
 351           " cur_eden: " SIZE_FORMAT
 352           " max_eden_size: " SIZE_FORMAT
 353           " avg_young_live: " SIZE_FORMAT,
 354           desired_eden_size, _eden_size, eden_limit, cur_eden,
 355           max_eden_size, (size_t)avg_young_live()->average());
 356   }
 357   if (gc_cost() > gc_cost_limit) {
 358     log_debug(gc, ergo)(
 359           "PSAdaptiveSizePolicy::compute_eden_space_size: gc time limit"
 360           " gc_cost: %f "
 361           " GCTimeLimit: " UINTX_FORMAT,
 362           gc_cost(), GCTimeLimit);
 363   }
 364 
 365   // Align everything and make a final limit check
 366   desired_eden_size  = align_up(desired_eden_size, _space_alignment);
 367   desired_eden_size  = MAX2(desired_eden_size, _space_alignment);
 368 
 369   eden_limit  = align_down(eden_limit, _space_alignment);
 370 
 371   // And one last limit check, now that we've aligned things.
 372   if (desired_eden_size > eden_limit) {
 373     // If the policy says to get a larger eden but
 374     // is hitting the limit, don't decrease eden.
 375     // This can lead to a general drifting down of the
 376     // eden size.  Let the tenuring calculation push more
 377     // into the old gen.
 378     desired_eden_size = MAX2(eden_limit, cur_eden);
 379   }
 380 
 381   log_debug(gc, ergo)("PSAdaptiveSizePolicy::compute_eden_space_size: costs minor_time: %f major_cost: %f mutator_cost: %f throughput_goal: %f",
 382              minor_gc_cost(), major_gc_cost(), mutator_cost(), _throughput_goal);
 383 
 384   log_trace(gc, ergo)("Minor_pause: %f major_pause: %f minor_interval: %f major_interval: %fpause_goal: %f",
 385                       _avg_minor_pause->padded_average(),
 386                       _avg_major_pause->padded_average(),
 387                       _avg_minor_interval->average(),
 388                       _avg_major_interval->average(),
 389                       gc_pause_goal_sec());
 390 
 391   log_debug(gc, ergo)("Live_space: " SIZE_FORMAT " free_space: " SIZE_FORMAT,
 392                       live_space(), free_space());
 393 
 394   log_trace(gc, ergo)("Base_footprint: " SIZE_FORMAT " avg_young_live: " SIZE_FORMAT " avg_old_live: " SIZE_FORMAT,
 395                       (size_t)_avg_base_footprint->average(),
 396                       (size_t)avg_young_live()->average(),
 397                       (size_t)avg_old_live()->average());
 398 
 399   log_debug(gc, ergo)("Old eden_size: " SIZE_FORMAT " desired_eden_size: " SIZE_FORMAT,
 400                       _eden_size, desired_eden_size);
 401 
 402   set_eden_size(desired_eden_size);
 403 }
 404 
 405 void PSAdaptiveSizePolicy::compute_old_gen_free_space(
 406                                            size_t old_live,
 407                                            size_t cur_eden,
 408                                            size_t max_old_gen_size,
 409                                            bool   is_full_gc) {
 410 
 411   // Update statistics
 412   // Time statistics are updated as we go, update footprint stats here
 413   if (is_full_gc) {
 414     // old_live is only accurate after a full gc
 415     avg_old_live()->sample(old_live);
 416   }
 417 
 418   // This code used to return if the policy was not ready , i.e.,
 419   // policy_is_ready() returning false.  The intent was that
 420   // decisions below needed major collection times and so could
 421   // not be made before two major collections.  A consequence was
 422   // adjustments to the young generation were not done until after
 423   // two major collections even if the minor collections times
 424   // exceeded the requested goals.  Now let the young generation
 425   // adjust for the minor collection times.  Major collection times
 426   // will be zero for the first collection and will naturally be
 427   // ignored.  Tenured generation adjustments are only made at the
 428   // full collections so until the second major collection has
 429   // been reached, no tenured generation adjustments will be made.
 430 
 431   // Until we know better, desired promotion size uses the last calculation
 432   size_t desired_promo_size = _promo_size;
 433 
 434   // Start eden at the current value.  The desired value that is stored
 435   // in _eden_size is not bounded by constraints of the heap and can
 436   // run away.
 437   //
 438   // As expected setting desired_eden_size to the current
 439   // value of desired_eden_size as a starting point
 440   // caused desired_eden_size to grow way too large and caused
 441   // an overflow down stream.  It may have improved performance in
 442   // some case but is dangerous.
 443   size_t desired_eden_size = cur_eden;
 444 
 445   // Cache some values. There's a bit of work getting these, so
 446   // we might save a little time.
 447   const double major_cost = major_gc_cost();
 448   const double minor_cost = minor_gc_cost();
 449 
 450   // Limits on our growth
 451   size_t promo_limit = (size_t)(max_old_gen_size - avg_old_live()->average());
 452 
 453   // But don't force a promo size below the current promo size. Otherwise,
 454   // the promo size will shrink for no good reason.
 455   promo_limit = MAX2(promo_limit, _promo_size);
 456 
 457   const double gc_cost_limit = GCTimeLimit/100.0;
 458 
 459   // Which way should we go?
 460   // if pause requirement is not met
 461   //   adjust size of any generation with average paus exceeding
 462   //   the pause limit.  Adjust one pause at a time (the larger)
 463   //   and only make adjustments for the major pause at full collections.
 464   // else if throughput requirement not met
 465   //   adjust the size of the generation with larger gc time.  Only
 466   //   adjust one generation at a time.
 467   // else
 468   //   adjust down the total heap size.  Adjust down the larger of the
 469   //   generations.
 470 
 471   // Add some checks for a threshold for a change.  For example,
 472   // a change less than the necessary alignment is probably not worth
 473   // attempting.
 474 
 475   if ((_avg_minor_pause->padded_average() > gc_pause_goal_sec()) ||
 476       (_avg_major_pause->padded_average() > gc_pause_goal_sec())) {
 477     //
 478     // Check pauses
 479     //
 480     // Make changes only to affect one of the pauses (the larger)
 481     // at a time.
 482     if (is_full_gc) {
 483       set_decide_at_full_gc(decide_at_full_gc_true);
 484       adjust_promo_for_pause_time(is_full_gc, &desired_promo_size, &desired_eden_size);
 485     }
 486   } else if (adjusted_mutator_cost() < _throughput_goal) {
 487     // This branch used to require that (mutator_cost() > 0.0 in 1.4.2.
 488     // This sometimes resulted in skipping to the minimize footprint
 489     // code.  Change this to try and reduce GC time if mutator time is
 490     // negative for whatever reason.  Or for future consideration,
 491     // bail out of the code if mutator time is negative.
 492     //
 493     // Throughput
 494     //
 495     assert(major_cost >= 0.0, "major cost is < 0.0");
 496     assert(minor_cost >= 0.0, "minor cost is < 0.0");
 497     // Try to reduce the GC times.
 498     if (is_full_gc) {
 499       set_decide_at_full_gc(decide_at_full_gc_true);
 500       adjust_promo_for_throughput(is_full_gc, &desired_promo_size);
 501     }
 502   } else {
 503 
 504     // Be conservative about reducing the footprint.
 505     //   Do a minimum number of major collections first.
 506     //   Have reasonable averages for major and minor collections costs.
 507     if (UseAdaptiveSizePolicyFootprintGoal &&
 508         young_gen_policy_is_ready() &&
 509         avg_major_gc_cost()->average() >= 0.0 &&
 510         avg_minor_gc_cost()->average() >= 0.0) {
 511       if (is_full_gc) {
 512         set_decide_at_full_gc(decide_at_full_gc_true);
 513         size_t desired_sum = desired_eden_size + desired_promo_size;
 514         desired_promo_size = adjust_promo_for_footprint(desired_promo_size, desired_sum);
 515       }
 516     }
 517   }
 518 
 519   // Note we make the same tests as in the code block below;  the code
 520   // seems a little easier to read with the printing in another block.
 521   if (desired_promo_size > promo_limit)  {
 522     // "free_in_old_gen" was the original value for used for promo_limit
 523     size_t free_in_old_gen = (size_t)(max_old_gen_size - avg_old_live()->average());
 524     log_debug(gc, ergo)(
 525           "PSAdaptiveSizePolicy::compute_old_gen_free_space limits:"
 526           " desired_promo_size: " SIZE_FORMAT
 527           " promo_limit: " SIZE_FORMAT
 528           " free_in_old_gen: " SIZE_FORMAT
 529           " max_old_gen_size: " SIZE_FORMAT
 530           " avg_old_live: " SIZE_FORMAT,
 531           desired_promo_size, promo_limit, free_in_old_gen,
 532           max_old_gen_size, (size_t) avg_old_live()->average());
 533   }
 534   if (gc_cost() > gc_cost_limit) {
 535     log_debug(gc, ergo)(
 536           "PSAdaptiveSizePolicy::compute_old_gen_free_space: gc time limit"
 537           " gc_cost: %f "
 538           " GCTimeLimit: " UINTX_FORMAT,
 539           gc_cost(), GCTimeLimit);
 540   }
 541 
 542   // Align everything and make a final limit check
 543   desired_promo_size = align_up(desired_promo_size, _space_alignment);
 544   desired_promo_size = MAX2(desired_promo_size, _space_alignment);
 545 
 546   promo_limit = align_down(promo_limit, _space_alignment);
 547 
 548   // And one last limit check, now that we've aligned things.
 549   desired_promo_size = MIN2(desired_promo_size, promo_limit);
 550 
 551   // Timing stats
 552   log_debug(gc, ergo)("PSAdaptiveSizePolicy::compute_old_gen_free_space: costs minor_time: %f major_cost: %f  mutator_cost: %f throughput_goal: %f",
 553              minor_gc_cost(), major_gc_cost(), mutator_cost(), _throughput_goal);
 554 
 555   log_trace(gc, ergo)("Minor_pause: %f major_pause: %f minor_interval: %f major_interval: %f pause_goal: %f",
 556                       _avg_minor_pause->padded_average(),
 557                       _avg_major_pause->padded_average(),
 558                       _avg_minor_interval->average(),
 559                       _avg_major_interval->average(),
 560                       gc_pause_goal_sec());
 561 
 562   // Footprint stats
 563   log_debug(gc, ergo)("Live_space: " SIZE_FORMAT " free_space: " SIZE_FORMAT,
 564                       live_space(), free_space());
 565 
 566   log_trace(gc, ergo)("Base_footprint: " SIZE_FORMAT " avg_young_live: " SIZE_FORMAT " avg_old_live: " SIZE_FORMAT,
 567                       (size_t)_avg_base_footprint->average(),
 568                       (size_t)avg_young_live()->average(),
 569                       (size_t)avg_old_live()->average());
 570 
 571   log_debug(gc, ergo)("Old promo_size: " SIZE_FORMAT " desired_promo_size: " SIZE_FORMAT,
 572                       _promo_size, desired_promo_size);
 573 
 574   set_promo_size(desired_promo_size);
 575 }
 576 
 577 void PSAdaptiveSizePolicy::decay_supplemental_growth(bool is_full_gc) {
 578   // Decay the supplemental increment?  Decay the supplement growth
 579   // factor even if it is not used.  It is only meant to give a boost
 580   // to the initial growth and if it is not used, then it was not
 581   // needed.
 582   if (is_full_gc) {
 583     // Don't wait for the threshold value for the major collections.  If
 584     // here, the supplemental growth term was used and should decay.
 585     if ((_avg_major_pause->count() % TenuredGenerationSizeSupplementDecay)
 586         == 0) {
 587       _old_gen_size_increment_supplement =
 588         _old_gen_size_increment_supplement >> 1;
 589     }
 590   } else {
 591     if ((_avg_minor_pause->count() >= AdaptiveSizePolicyReadyThreshold) &&
 592         (_avg_minor_pause->count() % YoungGenerationSizeSupplementDecay) == 0) {
 593       _young_gen_size_increment_supplement =
 594         _young_gen_size_increment_supplement >> 1;
 595     }
 596   }
 597 }
 598 
 599 void PSAdaptiveSizePolicy::adjust_eden_for_minor_pause_time(bool is_full_gc,
 600     size_t* desired_eden_size_ptr) {
 601 
 602   // Adjust the young generation size to reduce pause time of
 603   // of collections.
 604   //
 605   // The AdaptiveSizePolicyInitializingSteps test is not used
 606   // here.  It has not seemed to be needed but perhaps should
 607   // be added for consistency.
 608   if (minor_pause_young_estimator()->decrement_will_decrease()) {
 609         // reduce eden size
 610     set_change_young_gen_for_min_pauses(
 611           decrease_young_gen_for_min_pauses_true);
 612     *desired_eden_size_ptr = *desired_eden_size_ptr -
 613       eden_decrement_aligned_down(*desired_eden_size_ptr);
 614     } else {
 615       // EXPERIMENTAL ADJUSTMENT
 616       // Only record that the estimator indicated such an action.
 617       // *desired_eden_size_ptr = *desired_eden_size_ptr + eden_heap_delta;
 618       set_change_young_gen_for_min_pauses(
 619           increase_young_gen_for_min_pauses_true);
 620   }
 621 }
 622 
 623 void PSAdaptiveSizePolicy::adjust_promo_for_pause_time(bool is_full_gc,
 624                                              size_t* desired_promo_size_ptr,
 625                                              size_t* desired_eden_size_ptr) {
 626 
 627   size_t promo_heap_delta = 0;
 628   // Add some checks for a threshold for a change.  For example,
 629   // a change less than the required alignment is probably not worth
 630   // attempting.
 631 
 632   if (_avg_minor_pause->padded_average() <= _avg_major_pause->padded_average() && is_full_gc) {
 633     // Adjust for the major pause time only at full gc's because the
 634     // affects of a change can only be seen at full gc's.
 635 
 636     // Reduce old generation size to reduce pause?
 637     if (major_pause_old_estimator()->decrement_will_decrease()) {
 638       // reduce old generation size
 639       set_change_old_gen_for_maj_pauses(decrease_old_gen_for_maj_pauses_true);
 640       promo_heap_delta = promo_decrement_aligned_down(*desired_promo_size_ptr);
 641       *desired_promo_size_ptr = _promo_size - promo_heap_delta;
 642     } else {
 643       // EXPERIMENTAL ADJUSTMENT
 644       // Only record that the estimator indicated such an action.
 645       // *desired_promo_size_ptr = _promo_size +
 646       //   promo_increment_aligned_up(*desired_promo_size_ptr);
 647       set_change_old_gen_for_maj_pauses(increase_old_gen_for_maj_pauses_true);
 648     }
 649   }
 650 
 651   log_trace(gc, ergo)(
 652     "PSAdaptiveSizePolicy::adjust_promo_for_pause_time "
 653     "adjusting gen sizes for major pause (avg %f goal %f). "
 654     "desired_promo_size " SIZE_FORMAT " promo delta " SIZE_FORMAT,
 655     _avg_major_pause->average(), gc_pause_goal_sec(),
 656     *desired_promo_size_ptr, promo_heap_delta);
 657 }
 658 
 659 void PSAdaptiveSizePolicy::adjust_eden_for_pause_time(bool is_full_gc,
 660                                              size_t* desired_promo_size_ptr,
 661                                              size_t* desired_eden_size_ptr) {
 662 
 663   size_t eden_heap_delta = 0;
 664   // Add some checks for a threshold for a change.  For example,
 665   // a change less than the required alignment is probably not worth
 666   // attempting.
 667   if (_avg_minor_pause->padded_average() > _avg_major_pause->padded_average()) {
 668     adjust_eden_for_minor_pause_time(is_full_gc, desired_eden_size_ptr);
 669   }
 670   log_trace(gc, ergo)(
 671     "PSAdaptiveSizePolicy::adjust_eden_for_pause_time "
 672     "adjusting gen sizes for major pause (avg %f goal %f). "
 673     "desired_eden_size " SIZE_FORMAT " eden delta " SIZE_FORMAT,
 674     _avg_major_pause->average(), gc_pause_goal_sec(),
 675     *desired_eden_size_ptr, eden_heap_delta);
 676 }
 677 
 678 void PSAdaptiveSizePolicy::adjust_promo_for_throughput(bool is_full_gc,
 679                                              size_t* desired_promo_size_ptr) {
 680 
 681   // Add some checks for a threshold for a change.  For example,
 682   // a change less than the required alignment is probably not worth
 683   // attempting.
 684 
 685   if ((gc_cost() + mutator_cost()) == 0.0) {
 686     return;
 687   }
 688 
 689   log_trace(gc, ergo)("PSAdaptiveSizePolicy::adjust_promo_for_throughput(is_full: %d, promo: " SIZE_FORMAT "): mutator_cost %f  major_gc_cost %f minor_gc_cost %f",
 690                       is_full_gc, *desired_promo_size_ptr, mutator_cost(), major_gc_cost(), minor_gc_cost());
 691 
 692   // Tenured generation
 693   if (is_full_gc) {
 694     // Calculate the change to use for the tenured gen.
 695     size_t scaled_promo_heap_delta = 0;
 696     // Can the increment to the generation be scaled?
 697     if (gc_cost() >= 0.0 && major_gc_cost() >= 0.0) {
 698       size_t promo_heap_delta =
 699         promo_increment_with_supplement_aligned_up(*desired_promo_size_ptr);
 700       double scale_by_ratio = major_gc_cost() / gc_cost();
 701       scaled_promo_heap_delta =
 702         (size_t) (scale_by_ratio * (double) promo_heap_delta);
 703       log_trace(gc, ergo)("Scaled tenured increment: " SIZE_FORMAT " by %f down to " SIZE_FORMAT,
 704                           promo_heap_delta, scale_by_ratio, scaled_promo_heap_delta);
 705     } else if (major_gc_cost() >= 0.0) {
 706       // Scaling is not going to work.  If the major gc time is the
 707       // larger, give it a full increment.
 708       if (major_gc_cost() >= minor_gc_cost()) {
 709         scaled_promo_heap_delta =
 710           promo_increment_with_supplement_aligned_up(*desired_promo_size_ptr);
 711       }
 712     } else {
 713       // Don't expect to get here but it's ok if it does
 714       // in the product build since the delta will be 0
 715       // and nothing will change.
 716       assert(false, "Unexpected value for gc costs");
 717     }
 718 
 719     switch (AdaptiveSizeThroughPutPolicy) {
 720       case 1:
 721         // Early in the run the statistics might not be good.  Until
 722         // a specific number of collections have been, use the heuristic
 723         // that a larger generation size means lower collection costs.
 724         if (major_collection_estimator()->increment_will_decrease() ||
 725            (_old_gen_change_for_major_throughput
 726             <= AdaptiveSizePolicyInitializingSteps)) {
 727           // Increase tenured generation size to reduce major collection cost
 728           if ((*desired_promo_size_ptr + scaled_promo_heap_delta) >
 729               *desired_promo_size_ptr) {
 730             *desired_promo_size_ptr = _promo_size + scaled_promo_heap_delta;
 731           }
 732           set_change_old_gen_for_throughput(
 733               increase_old_gen_for_throughput_true);
 734               _old_gen_change_for_major_throughput++;
 735         } else {
 736           // EXPERIMENTAL ADJUSTMENT
 737           // Record that decreasing the old gen size would decrease
 738           // the major collection cost but don't do it.
 739           // *desired_promo_size_ptr = _promo_size -
 740           //   promo_decrement_aligned_down(*desired_promo_size_ptr);
 741           set_change_old_gen_for_throughput(
 742                 decrease_old_gen_for_throughput_true);
 743         }
 744 
 745         break;
 746       default:
 747         // Simplest strategy
 748         if ((*desired_promo_size_ptr + scaled_promo_heap_delta) >
 749             *desired_promo_size_ptr) {
 750           *desired_promo_size_ptr = *desired_promo_size_ptr +
 751             scaled_promo_heap_delta;
 752         }
 753         set_change_old_gen_for_throughput(
 754           increase_old_gen_for_throughput_true);
 755         _old_gen_change_for_major_throughput++;
 756     }
 757 
 758     log_trace(gc, ergo)("Adjusting tenured gen for throughput (avg %f goal %f). desired_promo_size " SIZE_FORMAT " promo_delta " SIZE_FORMAT ,
 759                         mutator_cost(),
 760                         _throughput_goal,
 761                         *desired_promo_size_ptr, scaled_promo_heap_delta);
 762   }
 763 }
 764 
 765 void PSAdaptiveSizePolicy::adjust_eden_for_throughput(bool is_full_gc,
 766                                              size_t* desired_eden_size_ptr) {
 767 
 768   // Add some checks for a threshold for a change.  For example,
 769   // a change less than the required alignment is probably not worth
 770   // attempting.
 771 
 772   if ((gc_cost() + mutator_cost()) == 0.0) {
 773     return;
 774   }
 775 
 776   log_trace(gc, ergo)("PSAdaptiveSizePolicy::adjust_eden_for_throughput(is_full: %d, cur_eden: " SIZE_FORMAT "): mutator_cost %f  major_gc_cost %f minor_gc_cost %f",
 777                       is_full_gc, *desired_eden_size_ptr, mutator_cost(), major_gc_cost(), minor_gc_cost());
 778 
 779   // Young generation
 780   size_t scaled_eden_heap_delta = 0;
 781   // Can the increment to the generation be scaled?
 782   if (gc_cost() >= 0.0 && minor_gc_cost() >= 0.0) {
 783     size_t eden_heap_delta =
 784       eden_increment_with_supplement_aligned_up(*desired_eden_size_ptr);
 785     double scale_by_ratio = minor_gc_cost() / gc_cost();
 786     assert(scale_by_ratio <= 1.0 && scale_by_ratio >= 0.0, "Scaling is wrong");
 787     scaled_eden_heap_delta =
 788       (size_t) (scale_by_ratio * (double) eden_heap_delta);
 789     log_trace(gc, ergo)("Scaled eden increment: " SIZE_FORMAT " by %f down to " SIZE_FORMAT,
 790                         eden_heap_delta, scale_by_ratio, scaled_eden_heap_delta);
 791   } else if (minor_gc_cost() >= 0.0) {
 792     // Scaling is not going to work.  If the minor gc time is the
 793     // larger, give it a full increment.
 794     if (minor_gc_cost() > major_gc_cost()) {
 795       scaled_eden_heap_delta =
 796         eden_increment_with_supplement_aligned_up(*desired_eden_size_ptr);
 797     }
 798   } else {
 799     // Don't expect to get here but it's ok if it does
 800     // in the product build since the delta will be 0
 801     // and nothing will change.
 802     assert(false, "Unexpected value for gc costs");
 803   }
 804 
 805   // Use a heuristic for some number of collections to give
 806   // the averages time to settle down.
 807   switch (AdaptiveSizeThroughPutPolicy) {
 808     case 1:
 809       if (minor_collection_estimator()->increment_will_decrease() ||
 810         (_young_gen_change_for_minor_throughput
 811           <= AdaptiveSizePolicyInitializingSteps)) {
 812         // Expand young generation size to reduce frequency of
 813         // of collections.
 814         if ((*desired_eden_size_ptr + scaled_eden_heap_delta) >
 815             *desired_eden_size_ptr) {
 816           *desired_eden_size_ptr =
 817             *desired_eden_size_ptr + scaled_eden_heap_delta;
 818         }
 819         set_change_young_gen_for_throughput(
 820           increase_young_gen_for_througput_true);
 821         _young_gen_change_for_minor_throughput++;
 822       } else {
 823         // EXPERIMENTAL ADJUSTMENT
 824         // Record that decreasing the young gen size would decrease
 825         // the minor collection cost but don't do it.
 826         // *desired_eden_size_ptr = _eden_size -
 827         //   eden_decrement_aligned_down(*desired_eden_size_ptr);
 828         set_change_young_gen_for_throughput(
 829           decrease_young_gen_for_througput_true);
 830       }
 831           break;
 832     default:
 833       if ((*desired_eden_size_ptr + scaled_eden_heap_delta) >
 834           *desired_eden_size_ptr) {
 835         *desired_eden_size_ptr =
 836           *desired_eden_size_ptr + scaled_eden_heap_delta;
 837       }
 838       set_change_young_gen_for_throughput(
 839         increase_young_gen_for_througput_true);
 840       _young_gen_change_for_minor_throughput++;
 841   }
 842 
 843     log_trace(gc, ergo)("Adjusting eden for throughput (avg %f goal %f). desired_eden_size " SIZE_FORMAT " eden delta " SIZE_FORMAT,
 844                         mutator_cost(), _throughput_goal, *desired_eden_size_ptr, scaled_eden_heap_delta);
 845 }
 846 
 847 size_t PSAdaptiveSizePolicy::adjust_promo_for_footprint(
 848     size_t desired_promo_size, size_t desired_sum) {
 849   assert(desired_promo_size <= desired_sum, "Inconsistent parameters");
 850   set_decrease_for_footprint(decrease_old_gen_for_footprint_true);
 851 
 852   size_t change = promo_decrement(desired_promo_size);
 853   change = scale_down(change, desired_promo_size, desired_sum);
 854 
 855   size_t reduced_size = desired_promo_size - change;
 856 
 857   log_trace(gc, ergo)(
 858     "AdaptiveSizePolicy::adjust_promo_for_footprint "
 859     "adjusting tenured gen for footprint. "
 860     "starting promo size " SIZE_FORMAT
 861     " reduced promo size " SIZE_FORMAT
 862     " promo delta " SIZE_FORMAT,
 863     desired_promo_size, reduced_size, change );
 864 
 865   assert(reduced_size <= desired_promo_size, "Inconsistent result");
 866   return reduced_size;
 867 }
 868 
 869 size_t PSAdaptiveSizePolicy::adjust_eden_for_footprint(
 870   size_t desired_eden_size, size_t desired_sum) {
 871   assert(desired_eden_size <= desired_sum, "Inconsistent parameters");
 872   set_decrease_for_footprint(decrease_young_gen_for_footprint_true);
 873 
 874   size_t change = eden_decrement(desired_eden_size);
 875   change = scale_down(change, desired_eden_size, desired_sum);
 876 
 877   size_t reduced_size = desired_eden_size - change;
 878 
 879   log_trace(gc, ergo)(
 880     "AdaptiveSizePolicy::adjust_eden_for_footprint "
 881     "adjusting eden for footprint. "
 882     " starting eden size " SIZE_FORMAT
 883     " reduced eden size " SIZE_FORMAT
 884     " eden delta " SIZE_FORMAT,
 885     desired_eden_size, reduced_size, change);
 886 
 887   assert(reduced_size <= desired_eden_size, "Inconsistent result");
 888   return reduced_size;
 889 }
 890 
 891 // Scale down "change" by the factor
 892 //      part / total
 893 // Don't align the results.
 894 
 895 size_t PSAdaptiveSizePolicy::scale_down(size_t change,
 896                                         double part,
 897                                         double total) {
 898   assert(part <= total, "Inconsistent input");
 899   size_t reduced_change = change;
 900   if (total > 0) {
 901     double fraction =  part / total;
 902     reduced_change = (size_t) (fraction * (double) change);
 903   }
 904   assert(reduced_change <= change, "Inconsistent result");
 905   return reduced_change;
 906 }
 907 
 908 size_t PSAdaptiveSizePolicy::eden_increment(size_t cur_eden,
 909                                             uint percent_change) {
 910   size_t eden_heap_delta;
 911   eden_heap_delta = cur_eden / 100 * percent_change;
 912   return eden_heap_delta;
 913 }
 914 
 915 size_t PSAdaptiveSizePolicy::eden_increment(size_t cur_eden) {
 916   return eden_increment(cur_eden, YoungGenerationSizeIncrement);
 917 }
 918 
 919 size_t PSAdaptiveSizePolicy::eden_increment_aligned_up(size_t cur_eden) {
 920   size_t result = eden_increment(cur_eden, YoungGenerationSizeIncrement);
 921   return align_up(result, _space_alignment);
 922 }
 923 
 924 size_t PSAdaptiveSizePolicy::eden_increment_aligned_down(size_t cur_eden) {
 925   size_t result = eden_increment(cur_eden);
 926   return align_down(result, _space_alignment);
 927 }
 928 
 929 size_t PSAdaptiveSizePolicy::eden_increment_with_supplement_aligned_up(
 930   size_t cur_eden) {
 931   size_t result = eden_increment(cur_eden,
 932     YoungGenerationSizeIncrement + _young_gen_size_increment_supplement);
 933   return align_up(result, _space_alignment);
 934 }
 935 
 936 size_t PSAdaptiveSizePolicy::eden_decrement_aligned_down(size_t cur_eden) {
 937   size_t eden_heap_delta = eden_decrement(cur_eden);
 938   return align_down(eden_heap_delta, _space_alignment);
 939 }
 940 
 941 size_t PSAdaptiveSizePolicy::eden_decrement(size_t cur_eden) {
 942   size_t eden_heap_delta = eden_increment(cur_eden) /
 943     AdaptiveSizeDecrementScaleFactor;
 944   return eden_heap_delta;
 945 }
 946 
 947 size_t PSAdaptiveSizePolicy::promo_increment(size_t cur_promo,
 948                                              uint percent_change) {
 949   size_t promo_heap_delta;
 950   promo_heap_delta = cur_promo / 100 * percent_change;
 951   return promo_heap_delta;
 952 }
 953 
 954 size_t PSAdaptiveSizePolicy::promo_increment(size_t cur_promo) {
 955   return promo_increment(cur_promo, TenuredGenerationSizeIncrement);
 956 }
 957 
 958 size_t PSAdaptiveSizePolicy::promo_increment_aligned_up(size_t cur_promo) {
 959   size_t result =  promo_increment(cur_promo, TenuredGenerationSizeIncrement);
 960   return align_up(result, _space_alignment);
 961 }
 962 
 963 size_t PSAdaptiveSizePolicy::promo_increment_aligned_down(size_t cur_promo) {
 964   size_t result =  promo_increment(cur_promo, TenuredGenerationSizeIncrement);
 965   return align_down(result, _space_alignment);
 966 }
 967 
 968 size_t PSAdaptiveSizePolicy::promo_increment_with_supplement_aligned_up(
 969   size_t cur_promo) {
 970   size_t result =  promo_increment(cur_promo,
 971     TenuredGenerationSizeIncrement + _old_gen_size_increment_supplement);
 972   return align_up(result, _space_alignment);
 973 }
 974 
 975 size_t PSAdaptiveSizePolicy::promo_decrement_aligned_down(size_t cur_promo) {
 976   size_t promo_heap_delta = promo_decrement(cur_promo);
 977   return align_down(promo_heap_delta, _space_alignment);
 978 }
 979 
 980 size_t PSAdaptiveSizePolicy::promo_decrement(size_t cur_promo) {
 981   size_t promo_heap_delta = promo_increment(cur_promo);
 982   promo_heap_delta = promo_heap_delta / AdaptiveSizeDecrementScaleFactor;
 983   return promo_heap_delta;
 984 }
 985 
 986 uint PSAdaptiveSizePolicy::compute_survivor_space_size_and_threshold(
 987                                              bool is_survivor_overflow,
 988                                              uint tenuring_threshold,
 989                                              size_t survivor_limit) {
 990   assert(survivor_limit >= _space_alignment,
 991          "survivor_limit too small");
 992   assert(is_aligned(survivor_limit, _space_alignment),
 993          "survivor_limit not aligned");
 994 
 995   // This method is called even if the tenuring threshold and survivor
 996   // spaces are not adjusted so that the averages are sampled above.
 997   if (!UsePSAdaptiveSurvivorSizePolicy ||
 998       !young_gen_policy_is_ready()) {
 999     return tenuring_threshold;
1000   }
1001 
1002   // We'll decide whether to increase or decrease the tenuring
1003   // threshold based partly on the newly computed survivor size
1004   // (if we hit the maximum limit allowed, we'll always choose to
1005   // decrement the threshold).
1006   bool incr_tenuring_threshold = false;
1007   bool decr_tenuring_threshold = false;
1008 
1009   set_decrement_tenuring_threshold_for_gc_cost(false);
1010   set_increment_tenuring_threshold_for_gc_cost(false);
1011   set_decrement_tenuring_threshold_for_survivor_limit(false);
1012 
1013   if (!is_survivor_overflow) {
1014     // Keep running averages on how much survived
1015 
1016     // We use the tenuring threshold to equalize the cost of major
1017     // and minor collections.
1018     // ThresholdTolerance is used to indicate how sensitive the
1019     // tenuring threshold is to differences in cost between the
1020     // collection types.
1021 
1022     // Get the times of interest. This involves a little work, so
1023     // we cache the values here.
1024     const double major_cost = major_gc_cost();
1025     const double minor_cost = minor_gc_cost();
1026 
1027     if (minor_cost > major_cost * _threshold_tolerance_percent) {
1028       // Minor times are getting too long;  lower the threshold so
1029       // less survives and more is promoted.
1030       decr_tenuring_threshold = true;
1031       set_decrement_tenuring_threshold_for_gc_cost(true);
1032     } else if (major_cost > minor_cost * _threshold_tolerance_percent) {
1033       // Major times are too long, so we want less promotion.
1034       incr_tenuring_threshold = true;
1035       set_increment_tenuring_threshold_for_gc_cost(true);
1036     }
1037 
1038   } else {
1039     // Survivor space overflow occurred, so promoted and survived are
1040     // not accurate. We'll make our best guess by combining survived
1041     // and promoted and count them as survivors.
1042     //
1043     // We'll lower the tenuring threshold to see if we can correct
1044     // things. Also, set the survivor size conservatively. We're
1045     // trying to avoid many overflows from occurring if defnew size
1046     // is just too small.
1047 
1048     decr_tenuring_threshold = true;
1049   }
1050 
1051   // The padded average also maintains a deviation from the average;
1052   // we use this to see how good of an estimate we have of what survived.
1053   // We're trying to pad the survivor size as little as possible without
1054   // overflowing the survivor spaces.
1055   size_t target_size = align_up((size_t)_avg_survived->padded_average(),
1056                                      _space_alignment);
1057   target_size = MAX2(target_size, _space_alignment);
1058 
1059   if (target_size > survivor_limit) {
1060     // Target size is bigger than we can handle. Let's also reduce
1061     // the tenuring threshold.
1062     target_size = survivor_limit;
1063     decr_tenuring_threshold = true;
1064     set_decrement_tenuring_threshold_for_survivor_limit(true);
1065   }
1066 
1067   // Finally, increment or decrement the tenuring threshold, as decided above.
1068   // We test for decrementing first, as we might have hit the target size
1069   // limit.
1070   if (decr_tenuring_threshold && !(AlwaysTenure || NeverTenure)) {
1071     if (tenuring_threshold > 1) {
1072       tenuring_threshold--;
1073     }
1074   } else if (incr_tenuring_threshold && !(AlwaysTenure || NeverTenure)) {
1075     if (tenuring_threshold < MaxTenuringThreshold) {
1076       tenuring_threshold++;
1077     }
1078   }
1079 
1080   // We keep a running average of the amount promoted which is used
1081   // to decide when we should collect the old generation (when
1082   // the amount of old gen free space is less than what we expect to
1083   // promote).
1084 
1085   log_trace(gc, ergo)("avg_survived: %f  avg_deviation: %f", _avg_survived->average(), _avg_survived->deviation());
1086   log_debug(gc, ergo)("avg_survived_padded_avg: %f", _avg_survived->padded_average());
1087 
1088   log_trace(gc, ergo)("avg_promoted_avg: %f  avg_promoted_dev: %f", avg_promoted()->average(), avg_promoted()->deviation());
1089   log_debug(gc, ergo)("avg_promoted_padded_avg: %f  avg_pretenured_padded_avg: %f  tenuring_thresh: %d  target_size: " SIZE_FORMAT,
1090                       avg_promoted()->padded_average(),
1091                       _avg_pretenured->padded_average(),
1092                       tenuring_threshold, target_size);
1093 
1094   set_survivor_size(target_size);
1095 
1096   return tenuring_threshold;
1097 }
1098 
1099 void PSAdaptiveSizePolicy::update_averages(bool is_survivor_overflow,
1100                                            size_t survived,
1101                                            size_t promoted) {
1102   // Update averages
1103   if (!is_survivor_overflow) {
1104     // Keep running averages on how much survived
1105     _avg_survived->sample(survived);
1106   } else {
1107     size_t survived_guess = survived + promoted;
1108     _avg_survived->sample(survived_guess);
1109   }
1110   avg_promoted()->sample(promoted);
1111 
1112   log_trace(gc, ergo)("AdaptiveSizePolicy::update_averages:  survived: "  SIZE_FORMAT "  promoted: "  SIZE_FORMAT "  overflow: %s",
1113                       survived, promoted, is_survivor_overflow ? "true" : "false");
1114 }
1115 
1116 bool PSAdaptiveSizePolicy::print() const {
1117 
1118   if (!UseAdaptiveSizePolicy) {
1119     return false;
1120   }
1121 
1122   if (AdaptiveSizePolicy::print()) {
1123     AdaptiveSizePolicy::print_tenuring_threshold(PSScavenge::tenuring_threshold());
1124     return true;
1125   }
1126 
1127   return false;
1128 }