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