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