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